error[internal]: left behind trailing whitespace
Minimum reproducible example:
fn main() {
let foo =
// 114514
if true {
1919
} else {
810
};
}
Note the extra whitespace after =.
Version: 1.7.0-stable
https://github.com/rust-lang/rustfmt/issues/2896 ?
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.
@rustbot claim
@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
Closing my PR as https://github.com/rust-lang/rustfmt/pull/5501/files is addressing the same issue
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 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.
Seeing the same thing now with this repro. Seems like it can compile just fine and Clippy also passes it.