Skip to content

Only succeeds in formatting if there is a comment #6977

Description

@PilkeySEK

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-commentsArea: commentsA-stringsArea: string literalsC-bugCategory: this is a bug; use also I-* labels for specific bug kinds, e.g. I-non-idempotency or I-ICEI-poor-formattingIssue: poor formatting

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions