Skip to content
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

Formatting error ('left behind trailing whitespace') on if statement with comment between && and expression #6342

Closed
antonilol opened this issue Sep 21, 2024 · 1 comment
Labels
a-binop a-comments duplicate e-trailing whitespace error[internal]: left behind trailing whitespace

Comments

@antonilol
Copy link

antonilol commented Sep 21, 2024

Reproducer:

fn fn1() {
    #[derive(PartialEq)]
    struct Struct {
        looooooooooooooong_field_name: u8,
    }


    if None
                                    == Some(Struct {
                                        looooooooooooooong_field_name: 1, 
                                    })
                                    &&
                                    // removing this comment makes it format as usual
                                     true
    {
        todo!();
    }
}

(This is a minimal example of the format error made from real code, this is why there is a nonsensical None == Some(...) comparison, && true and a todo!())

Trying to use rustfmt (through cargo fmt) on this results in an error and the code being partially formatted.

cargo fmt --check result:

Diff in /tmp/rustfmt_bug/src/lib.rs:4:
         looooooooooooooong_field_name: u8,
     }
 
-
     if None
                                     == Some(Struct {
                                         looooooooooooooong_field_name: 1, 
error[internal]: left behind trailing whitespace
 --> /tmp/rustfmt_bug/src/lib.rs:9:9:74
  |
9 |                                         looooooooooooooong_field_name: 1, 
  |                                                                          ^
  |

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

I don't know if the long field name has anything to do with this, but the comment certainly does, as removing it will make it format correctly.

Removing the trailing space manually will make rustfmt not format but still return with exit code 0.

@ytmimi
Copy link
Contributor

ytmimi commented Sep 22, 2024

The trailing whitespace issue is a result of not handling comments in binary operators. #3591 (comment). rustfmt leaves the code as is to prevent it from removing the comment. If you remove the comment you'll see that rustfmt properly formats this:

fn fn1() {
    #[derive(PartialEq)]
    struct Struct {
        looooooooooooooong_field_name: u8,
    }

    if None
        == Some(Struct {
            looooooooooooooong_field_name: 1,
        })
        && true
    {
        todo!();
    }
}

Also going to close this one since it's effectively a duplicate of #3591 and #3167

@ytmimi ytmimi closed this as not planned Won't fix, can't repro, duplicate, stale Sep 22, 2024
@ytmimi ytmimi added duplicate a-comments a-binop e-trailing whitespace error[internal]: left behind trailing whitespace labels Sep 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a-binop a-comments duplicate e-trailing whitespace error[internal]: left behind trailing whitespace
Projects
None yet
Development

No branches or pull requests

2 participants