rustfmt fail to format if there is a multi-line comment at the end of the match expression and it is terminated by a comma

Created on 3 Feb 2020  Β·  2Comments  Β·  Source: rust-lang/rustfmt

Summary

If there is a multi-line comment after the last pattern in the match expression and it is terminated by a comma, the match expression cannot be formatted.

Also, if a line containing only spaces is found in the match expression, rustfmt will print an internal error and warning.

Occurs in both stable and nightly. The version is as follows.

$ rustfmt --version && rustfmt +nightly --version
rustfmt 1.4.11-stable (1838235 2019-12-03)
rustfmt 1.4.11-nightly (1838235 2019-12-03)

Formattable code

Before

fn f() {⏎
    let x = 0;⏎
    match x {⏎
        0 => {}⏎
⏎
⏎
        1 => {}⏎
        _ => {}⏎
⏎
⏎
        // foo⏎
        // bar⏎
    }⏎
}⏎

After

fn f() {⏎
    let x = 0;⏎
    match x {⏎
        0 => {}⏎
⏎
        1 => {}⏎
        _ => {} // foo⏎
                // bar⏎
    }⏎
}⏎

Unformattable code without an internal error and warning

fn f() {⏎
    let x = 0;⏎
    match x {⏎
        0 => {}⏎
⏎
⏎
        1 => {}⏎
        _ => {}⏎
⏎
⏎
        // foo⏎
        // bar,⏎
    }⏎
}⏎

Unformattable code with an internal error and warning

The space line can be anywhere in the match expression.

fn f() {⏎
    let x = 0;⏎
    match x {⏎
        ⏎
        0 => {}⏎
        ⏎
        2 => {}⏎
        _ => {}⏎
        ⏎
        // foo⏎
        // bar,⏎
        ⏎
    }⏎
}⏎
$ rustfmt +nightly ./test.rs 
error[internal]: left behind trailing whitespace
 --> test.rs:4:4:0
  |
4 |         
  | ^^^^^^^^
  |

error[internal]: left behind trailing whitespace
 --> test.rs:6:6:0
  |
6 |         
  | ^^^^^^^^
  |

error[internal]: left behind trailing whitespace
 --> test.rs:9:9:0
  |
9 |         
  | ^^^^^^^^
  |

error[internal]: left behind trailing whitespace
  --> test.rs:12:12:0
   |
12 |         
   | ^^^^^^^^
   |

warning: rustfmt has failed to format. See previous 4 errors.
a-comments a-matches bug

Most helpful comment

Yikes! This is reproducible on master/v2.0.0-rc.1-nightly as well

All 2 comments

Yikes! This is reproducible on master/v2.0.0-rc.1-nightly as well

I now understand that the internal error "left behind trailing whitespace" is output as a result of the validation of a piece of code that failed to format and is not the essence of this issue, sorry.

Was this page helpful?
0 / 5 - 0 ratings