rust-lang / rustfmt

Format Rust code
https://rust-lang.github.io/rustfmt/
Apache License 2.0
6.06k stars 890 forks source link

A long string in a match arm breaks formatting for the whole match expression #4797

Open arnaudgolfouse opened 3 years ago

arnaudgolfouse commented 3 years ago

Describe the bug

If an arm of a match expression contains a string with >= 87 characters, formatting fails on the whole match

To Reproduce

match 0 {
    0 => {"hello"} 1 => { ""}
      _ => {"veeeeeeeeeeery loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong string"}
};

Expected behavior

 match 0 {
    0 => "hello",
    1 => "",
    _ => {
        "veeeeeeeeeeery loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong string"
    }
};

If the third string is less than 86 characters, formatting works as expected.

Meta

calebcartwright commented 3 years ago

Thanks for reaching out, this is in the same boat as #4800 and is also fixed in source (apparently) but hasn't been pulled into a release branch.

For anyone interested in working on this please note that the fix would need to be versioned gated to avoid violating the formatting stability guarantee. It was an intentional decision early on in rustfmt to bail in cases like this when it's completely impossible to format an element within the max_width constraint, so even though it's preferable now to format anyway (and let users turn on config options like error_on_line_overflow) we still have to gate that change

ChinYing-Li commented 3 years ago

@calebcartwright I'd like to help with back-porting this.

calebcartwright commented 3 years ago

@calebcartwright I'd like to help with back-porting this.

Brilliant, thanks!

ChinYing-Li commented 3 years ago

Should I back-port to branch 1.4.36, or 1.4.37?