Only succeeds in formatting if there is a comment
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 7k
- Forks
- 1.1k
- Avg merge
- 2d 13h
- Merged PRs (30d)
- 24
Description
Summary
Rustfmt only succeeds in formatting this code if there is a comment following the long string:
fn fails() {
struct SomeStruct;
impl SomeStruct {
fn foo(&self, _s: String) {}
}
let some_struct = SomeStruct;
// This will not be formatted at all
some_struct.foo(format!(
"{}", if true {
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
} else {
""
},
));
}
fn succeeds() {
struct SomeStruct;
impl SomeStruct {
fn foo(&self, _s: String) {}
}
let some_struct = SomeStruct;
// This will be properly formatted
some_struct.foo(format!(
"{}", if true {
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" // this is a comment
} else {
""
},
));
}
Expected behavior
I expected to see this happen: Rustfmt should format the code in both macro invocations (fixing the indentation and moving the if statement to a new line.
Actual behavior
Instead, this happened: It fails silently in the fails() function but succeeds in the succeeds() function.
Configuration
rustfmt cli options used (if applicable): /
rustfmt configuration file (e.g. rustfmt.toml, if applicable): /
Reproduction Steps
Run rustfmt on the provided code, no other special steps are needed.
Playground link: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=ecdb07731fe1fc947c0a3a721a76e279
Meta
rustfmt --version:
rustfmt 1.9.0-stable (8bab26f4f6 2026-07-14)
cargo +nightly fmt --version:
rustfmt 1.10.0-nightly (89c61a7545 2026-07-23)
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 running the rustfmt CLI on the provided Rust reproducer and compare the fails() and succeeds() macro invocations. Trace the rustfmt entry point that formats this expression and add a regression test for the comment-free case. Done means both invocations are formatted with corrected indentation and the if statement moved to a new line.
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
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 65/100