rust-lang / rustfmt

Format Rust code
https://rust-lang.github.io/rustfmt/
Apache License 2.0
5.85k stars 860 forks source link

failed to format array with long string/identifier #6218

Closed xxchan closed 6 days ago

xxchan commented 6 days ago
❯ cargo +nightly fmt
error[internal]: left behind trailing whitespace
 --> /private/tmp/tmp/src/main.rs:6:6:1
  |
6 |           
  | ^^^^^^^^^^
  |
fn main() {
    let aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =
        1;
    let b = 2;
    let a = &[

                aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
        b,
    ];

    // or this
    // let a = &[

    //             "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
    //     "b",
    // ];
}
ytmimi commented 6 days ago

The max_width is a hard constraint for rustfmt. If it can't rewrite the identifier it'll fail to rewrite the let binding. Similarly, the long identifier in the list prevents it from being rewritten.

In this case there are two options to address the trailing whitespace error:

  1. The developer can manually remove the trailing whitespace
  2. increase the max_width to some sufficiently high value.