`cfg_select!` in expr position broken after formatting
Open
Nobody has claimed this yet.
A-macros
C-bug
I-invalid-code
I-unexpectedly-removes-code
- Dominant language
- Rust
- Stars
- 7k
- Forks
- 1.1k
- Avg merge
- 2d 13h
- Merged PRs (30d)
- 24
Description
Summary
I tried to format this code:
fn main() {
let a = cfg_select! {
feature = "foo" => {
{
let bar = "foo";
bar.to_owned()
}
}
_ => String::new(),
};
println!("{a}");
}
Expected behavior
I expected to see this happen:
Either:
- nothing
- reformat happens without affecting the number of braces
Actual behavior
Instead, this happened: rustfmt removed the braces, leading to incorrect syntax:
fn main() {
let a = cfg_select! {
feature = "foo" => {
let bar = "foo";
bar.to_owned()
}
_ => String::new(),
};
println!("{a}");
}
It's now effectively generating let a = let bar = "foo"; ...:
error: expected expression, found `let` statement
--> src/main.rs:4:13
|
4 | let bar = "foo";
| ^^^
|
= note: only supported directly in conditions of `if` and `while` expressions
error: macro expansion ignores `bar` and any tokens following
--> src/main.rs:5:13
|
2 | let a = cfg_select! {
| _____________-
3 | | feature = "foo" => {
4 | | let bar = "foo";
5 | | bar.to_owned()
| | ^^^
6 | | }
7 | | _ => String::new(),
8 | | };
| |_____- caused by the macro expansion here
|
= note: the usage of `cfg_select!` is likely invalid in expression context
error: trailing semicolon in macro used in expression position
--> src/main.rs:4:28
|
4 | let bar = "foo";
| ^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #79813 <https://github.com/rust-lang/rust/issues/79813>
= note: `#[deny(semicolon_in_expressions_from_macros)]` (part of `#[deny(future_incompatible)]`) on by default
Configuration
Default configuration
Meta
Checked both locally and on rust playground (same build):
1.100.0-nightly
(2026-08-21 c656540d6467dee1381f)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the provided Rust example with rustfmt using the linked Rust Playground configuration, then inspect how cfg_select! branches are formatted in expression position. Done means formatting no longer removes braces in a way that changes the generated syntax, while the reproducer remains valid.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Active
- Clarity
- Clearly specified
- Newbie friendliness
- 72/100