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)
Summary
Rustfmt only succeeds in formatting this code if there is a comment following the long string:
Expected behavior
I expected to see this happen: Rustfmt should format the code in both macro invocations (fixing the indentation and moving the
ifstatement to a new line.Actual behavior
Instead, this happened: It fails silently in the
fails()function but succeeds in thesucceeds()function.Configuration
rustfmtcli options used (if applicable): /rustfmtconfiguration 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:cargo +nightly fmt --version: