rustfmt: rustfmt formats different arms of the same match inconsistently
The big rustc format made some code I care about much less readable. Here’s an example:
- ref mut local @ LocalValue::Live(Operand::Immediate(_)) |
- ref mut local @ LocalValue::Uninitialized => {
- Ok(Ok(local))
- }
+ ref mut local @ LocalValue::Live(Operand::Immediate(_))
+ | ref mut local @ LocalValue::Uninitialized => Ok(Ok(local)),
The actual code running here for this branch got moved onto the same line as the last line of a pattern. That makes it quite hard to visually identify this code – compared to the old version of the code, the visual separation of pattern and code got lost.
About this issue
- Original URL
- State: open
- Created 7 years ago
- Reactions: 20
- Comments: 19 (15 by maintainers)
More generally, I disagree with the way rustfmt eagerly turns
into
and it also turns
into
In a long
match, I think it is a good idea to consistently either have pattern and expression on the same line, or on different lines, for all arms in that match. The visual similarities make it much easier to parse this code. But unfortunately, running rustfmt on such manually formatted code turns something symmetric into an inconsistent chaos. One really bad example is this: before -> after. (Yes, thewrite!arguments were not formatted properly before, and I am totally fine with those being formatted like rustfmt does. This is about the match arms, not thewrite!arguments.)IMO, rustfmt should honor my choice to start
exprin a new line, and never remove that newline. That would make it maintain good manual formatting, unlike now where it actively makes code less consistently formatted.Alternatively, if you want to enforce some rule, my proposal would be to only ever have
expron the same line aspatif we can do this for every branch of thematch(so all branches have just one pattern, and the expression is short enough). That would ensure consistency.👍
This is my core complaint with rustfmt, I think. It seems to regularly lose consistency between items in sequences, be that array literals, function arguments, match arms, whatever. I’d definitely like to see it make consistent choices across them, rather than the ones that today seem to be made in isolation.
I have opened an issue in the style-team repo: https://github.com/rust-lang/style-team/issues/196. However, I don’t know how active the style team is right now.
That’s not the right way to talk to other people, please rethink your choice of words. Maybe cool down for an hour or two before pressing the “submit” button when you are writing a bugreport in frustration.
I think most people will reasonably assume that the rustfmt issue tracker is the place to file problems with the way rustfmt formats, without caring much whether the source for this behavior is the style guide, or a rustfmt bug where it doesn’t match the style guide. In the rustc repo, we don’t use completely different issue trackers for “the compiler doesn’t follow documented behavior” and “the compiler follows documented behavior but I think that is the wrong behavior”. We generally can’t expect users to even know which of these categories their issue falls into. So it’s a bit surprising to see this split on the rustfmt/style side, and it will surely lead to users reporting issues in the wrong repo.
Just to add a differing viewpoint - I actually prefer in many cases that rustfmt does what it currently does re: making some statements on one line vs. making all multiline. For things like
Displayimpls on larger enums, it often seems like there are only one or two long/multiline blocks, and the rest fit on one. If having a single multiline block forced all to go multiline, vertical space of the match statement is effectively tripled.https://github.com/pluots/zspell/blob/71ae77932f2cc67f593d846a26a9ffd7cf6d0412/crates/zspell/src/affix.rs#L276-L367 for example, I agree with what rustfmt does. 61 matches fit on 91 lines this way, but forcing them to braced multiline would make the total double to 183 lines. (could have been smaller if I used
use Enum::*, but that’s beside the point).My personal rule would be that the default should be whichever matches 50% of the statements; if a majority fit on one line, make that the default and have others be an exception. If a majority take >1 line, then make them all be that way. Now of course that can’t actually be a rule (code churn…) But I don’t think that 1 or 2 multiline expressions should change entire large match statements (I guess that would cause churn too).
Haven’t seen this discussed really though, so maybe it’s not in the question anymore. Just wanted to add some 2¢.
Is there a process to propose amending the style guide? I can imagine something like saying that all match arms should be of the formatting style of the biggest of them, in order to ensure consistency.
This seems like an unnecessary and unhelpful characterization. Your snippet isn’t complete and directly reproducible so I’ll assume you are running rustfmt with the default configuration values, but the behavior you are complaining about isn’t related to match expression formatting regardless.
To demonstrate, let’s show the tuple without any match context:
And then let’s consider the relevant rules dictated by the Rust Style Guide The rules for tuples state:
So we look at the rules for function calls:
The threshold for whether a function call (or tuple) qualifies as “small” is controlled by the fn_call_width setting that has a default value of
60. The tuple in this example, and most of the tuples you had in your match arms are all longer than 60, and are therefore formatted as multilines.The Style Guide rules for match expressions does not have a clause that says “but ignore all the other rules in this style guide to keep visual consistency across match arms”.
There is nothing “psycho” here… rustfmt is simply, and correctly, applying the Style Guide rules for tuples
Any update on that? Looking at the close issue above, it seems that the question is more at the level of the style guide rather than in the implementation of rustfmt, but nonetheless as a user I find it very annoying that different arms of match get formatted differently: this breaks symmetry and leads to less readable code IMHO.
Turns out this was already a concern back when the formatting got implemented:
AFAIK, no such option exists yet. And indeed I agree entirely. I’d maybe add one thing: even when none of the branches are wrapped, there’s still the choice of putting the expression on the same line as the
=>, or into a new line. The simplest solution would be to always put it on a new line – I think that would overall be an improvement today, though some matches would suffer. If we go with having two styles of non-wrapped match arms, then that, too, should be consistent within a match: either all arms should use the line of the=>, or none of them should.Here’s an example diff of what rustfmt does where I disagree with every single change it is making: https://github.com/rust-lang/rust/pull/67833/commits/ff5b102e008dde17a8ec0ed0c364ac647d835a3e. These all introduce inconsistencies in match formatting.
And here’s how I manually formatted a long match, I had to disable rustfmt for that as there seems to be no way to make rustfmt not destroy the formatting: https://github.com/rust-lang/rust/pull/67833/commits/e44f3e7cbe340b5e12704091d72dc7b1620a4e45