rustfmt icon indicating copy to clipboard operation
rustfmt copied to clipboard

error[internal]: left behind trailing whitespace

Open QuarticCat opened this issue 1 year ago • 8 comments

Minimum reproducible example:

fn main() {
    let foo = 
    // 114514
    if true {
        1919
    } else {
        810
    };
}

Note the extra whitespace after =.

Version: 1.7.0-stable

QuarticCat avatar Jul 19 '24 08:07 QuarticCat

https://github.com/rust-lang/rustfmt/issues/2896 ?

asukaminato0721 avatar Jul 19 '24 09:07 asukaminato0721

There is trailing whitespace in the input snippet. The comment // 114514 prevents formatting, which also stops rustfmt from rewriting the assignment and removing the whitespace.

ytmimi avatar Jul 19 '24 13:07 ytmimi

@rustbot claim

johnhuichen avatar Aug 11 '24 16:08 johnhuichen

@ytmimi

I managed to find a fix for this scenario by applying rewrite_assign_rhs_with_comments.

The problem is rewrite_assign_rhs_with_comments seems to work different when the line is super long. Would appreciate some pointers

johnhuichen avatar Aug 11 '24 18:08 johnhuichen

Closing my PR as https://github.com/rust-lang/rustfmt/pull/5501/files is addressing the same issue

johnhuichen avatar Aug 16 '24 02:08 johnhuichen

Can someone help us understand why extra trailing whitespace is ever an error in a code formatter? I searched the issues here for "left behind trailing whitespace" and there are a ton open. It seems if this one issue were addressed, a whole slew of other issues would be addressed. And it's an internal error, so there is no feedback in the editor! Shouldn't extra whitespace be fixed automatically by rustfmt?

tgrushka avatar Dec 24 '24 19:12 tgrushka

@tgrushka The "left behind trailing whitespace" error is a general error message, and pops up for various different reasons. While trailing whitespace isn't inherently wrong, rustfmt tries to remove it during formatting, and flags an error if it introduces any trailing whitespace into the formatted output. Many of these errors messages are false positives, and often occur when rustfmt silently fails to format code with existing trailing whitespace (like this issue). Unfortunately, rustfmt doesn't distinguish between whitespace it added and that which was already in the source code. In the later case, I think a more specific warning would be less confusing for end users.

ytmimi avatar Jan 02 '25 17:01 ytmimi

Seeing the same thing now with this repro. Seems like it can compile just fine and Clippy also passes it.

connernilsen avatar Nov 26 '25 03:11 connernilsen