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

Trailing whitespace of match arm not stripped when the body expression is very long and follows in the second line #5689

Open
jinohkang-theori opened this issue Feb 9, 2023 · 5 comments
Labels
a-comments a-matches match arms, patterns, blocks, etc e-trailing whitespace error[internal]: left behind trailing whitespace p-low

Comments

@jinohkang-theori
Copy link

Minimal reproducer:

fn this_is_a_very_long_name_to_lengthen_the_expression_so_that_rustfmt_does_not_join_the_lines() {}

fn main() {
    match () {
        _ => /* (whitespace here) => */ 
            this_is_a_very_long_name_to_lengthen_the_expression_so_that_rustfmt_does_not_join_the_lines(),
    }
}

Output:

error[internal]: left behind trailing whitespace
 --> stdin:5:5:40
  |
5 |         _ => /* (whitespace here) => */ 
  |                                        ^
  |

warning: rustfmt has failed to format. See previous 1 errors.
@ytmimi ytmimi added p-low a-matches match arms, patterns, blocks, etc e-trailing whitespace error[internal]: left behind trailing whitespace labels Feb 9, 2023
@ytmimi
Copy link
Contributor

ytmimi commented Feb 9, 2023

Thanks for reaching out.

There are 2 issues here.

First, rustfmt is unable to format the match arm within the max_width constraint and therefore doesn't reformat the match at all, leaving the trailing space after the block comment unchanged. In this case the error is rustfmts way of saying there's whitesapce that it can't remove and is informing the developer that manual intervention is necessary.

The typical solution in these case is to increase the max_width to some sufficiently large value, however that won't work in this case because increasing the max width to allow the long function call to be placed on the same line as the => will result in the following error:

error[internal]: not formatted because a comment would be lost
 --> <stdin>:2
  |
2 |     match () {
  |
  = note: set `error_on_unformatted = false` to suppress the warning against comments or string literals

As described by the error, rustfmt isn't expecting a comment between the => and the match arm expression. One solution to resolve this error is to write the match arm with a block.

fn this_is_a_very_long_name_to_lengthen_the_expression_so_that_rustfmt_does_not_join_the_lines() {}

Input

fn main() {
    match () {
        _ => {/* (whitespace here) => */ 
            this_is_a_very_long_name_to_lengthen_the_expression_so_that_rustfmt_does_not_join_the_lines()
        }
    }
}

Output (using max_width=105)

fn main() {
    match () {
        _ => {
            /* (whitespace here) => */
            this_is_a_very_long_name_to_lengthen_the_expression_so_that_rustfmt_does_not_join_the_lines()
        }
    }
}

@ytmimi
Copy link
Contributor

ytmimi commented Feb 9, 2023

The comment error described above is better illustrated by this example, which uses a short function name:

fn main() {
    match () {
        _ => /* (whitespace here) => */ 
            f(),
    }
}

When running rustfmt with error_on_unformatted=true we get the following error:

error[internal]: not formatted because a comment would be lost
 --> <stdin>:2
  |
2 |     match () {
  |
  = note: set `error_on_unformatted = false` to suppress the warning against comments or string literals

error[internal]: left behind trailing whitespace
 --> <stdin>:3:3:40
  |
3 |         _ => /* (whitespace here) => */ 
  |                                        ^
  |

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

@mk12
Copy link

mk12 commented Mar 9, 2023

I'm seeing this error as well.

First, rustfmt is unable to format the match arm within the max_width constraint and therefore doesn't reformat the match at all, leaving the trailing space after the block comment unchanged. In this case the error is rustfmts way of saying there's whitspace that it can't remove is informing the developer that manual intervention is necessary.

That's surprising to me. error[internal] makes me think I'm hitting a bug in rustfmt. It would nice if the error message said this is happening because error_on_unformatted is enabled, and that you should disable it or increase max_width.

Edit: Oh, I see it does mention error_on_unformatted in your first example. Here is an example where it only prints the second error with no mention of configs:

fn f() {
    match x {
        0 => S {
            
        },
        _ => AAAAAAAAA,
    }
}

Result:

$ rustfmt file.rs --config max_width=20
error[internal]: left behind trailing whitespace
 --> /usr/local/google/home/mkember/a.rs:4:4:1
  |
4 |
  | ^^^^^^^^^^^^
  |

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

Edit again: Oh never mind, it still fails with error_on_unformatted=false. So this does look like a bug to me.

@ytmimi
Copy link
Contributor

ytmimi commented Mar 9, 2023

@mk12 could you post the actual code snippet where you're running into the warning. There are many different scenarios where error[internal]: left behind trailing whitespace warnings pop up.

In case your issue doesn't relate to comments between => and match arm expressions then it's probably best to file a separate issue.

Edit again: Oh never mind, it still fails with error_on_unformatted=false. So this does look like a bug to me.

Just to be clear It's only a bug if rustfmt is the one introducing trailing whitespace into the code

@mk12
Copy link

mk12 commented Mar 9, 2023

@ytmimi I filed #5711.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a-comments a-matches match arms, patterns, blocks, etc e-trailing whitespace error[internal]: left behind trailing whitespace p-low
Projects
None yet
Development

No branches or pull requests

3 participants