-
Notifications
You must be signed in to change notification settings - Fork 888
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
rustfmt adds useless brackets to unit value in match when pattern is too long #5134
Comments
@zombiepigdragon consider using this, which is equivalent: fn demo() {
if a {
if b {
match c {
DemoItemA
if example_var == "val1" && example_var_2 == "get to the column limit" => {}
}
}
}
} That's what that clippy lint is trying to suggest. assert_eq!((), {}); |
@bddap Thank you for clarifying what the clippy lint was pointing out! @zombiepigdragon I think that's the approach you should take. If you're using nightly rustfmt you might also consider using the match_arm_blocks = false configuration option. When you get a chance, can you confirm if either of these approaches helped? |
@bddap Yes, that does help. Thank you! I've switched it over for the time being. |
No. Correct/expected formatting isn't based on any subjective preference, but on the rules codified in the Rust Style Guide which are very explicit and prescriptive about all scenarios including this one https://github.com/rust-dev-tools/fmt-rfcs/blob/master/guide/expressions.md#match Particularly take note of the following lines from the guide:
rustfmt remains highly configurable because everyone recognizes that few topics around styling will receive ubiquitous agreement, and that's why options like Generally speaking, the style guide and rustfmt are focused on how the code written by the developer (or more specifically the various representations of code in the AST) should be formatted as opposed to what code the developer should write in cases where there's several syntactically viable alternatives. Whether you want the empty/unit tuple for that arm to be explicit or implicit is really up to you. We may incorporate some more explicit guidance specifically for the case of such match arms (refs rust-lang/style-team#146) and that is leaning towards the implicit route, though in the same theme as described above we'd likely have a config option in rustfmt that allows flexibility for user preference. |
Thanks again for reaching out, but going to close given the above context on why the behavior is correct along with options/workarounds for alternatives |
A simple reproducing case (assumed to be the correct formatting) is
which
rustfmt
will format toThis is particularly irritating as it triggers a clippy lint, leaving the code with a choice between being correct in clippy's or rustfmt's eyes. Without looking into the rustfmt source to troubleshoot, it seems as if rustfmt is generating the line with the brackets, seeing it passes the column limit, and laying out the code accordingly instead of removing the useless brackets.
Tested in versions
rustfmt 1.4.37-stable (f1edd04 2021-11-29)
andrustfmt 1.4.38-nightly (0b42dea 2021-12-09)
The text was updated successfully, but these errors were encountered: