Skip to content

Formatting for trailing whitespace failed on comment separated multi-line expression #5981

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
theatischbein opened this issue Dec 14, 2023 · 1 comment

Comments

@theatischbein
Copy link

I ended up with the following code, which will fail to format.

fn main() {
    let x = 1234 
        // important command!
        + 567;
    println!("x: {}", x);
}

The code compiles and runs correctly.

Cargo is also unable to format this code:

❯ cargo fmt
error[internal]: left behind trailing whitespace
 --> /tmp/testing_rust/src/main.rs:2:2:17
  |
2 |     let x = 1234 
  |                 ^
  |

warning: rustfmt has failed to format. See previous 1 errors.

The problem does not occur and the formatting works if I move the comment out of the multiline addition.

fn main() {
    // important command!
    let x = 1234 
        + 567;
    println!("x: {}", x);
}

I can see that this may not be good code style, but it should not cause the file formatting to fail.

rustfmt version: 1.6.0-nightly (28eb857 2023-08-12)

rustc version: 1.73.0-nightly (28eb857b9 2023-08-12)

relevant settings: none

@ytmimi
Copy link
Contributor

ytmimi commented Dec 16, 2023

Thanks for reaching out. This is a duplicate of #3167.

rustfmt doesn't handle comments in the middle of binary expressions very well. Instead of removing the comment, rustfmt opts to leave the entire AST node unformatted. In this case your original code snippet had trailing whitespace that rustfmt was unable to remove given that it couldn't rewrite the binary expression, and the only way to prevent the warning is to manually remove the whitespace.

@ytmimi ytmimi closed this as not planned Won't fix, can't repro, duplicate, stale Dec 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants