Format match exprs even when or-patterns have comments#6893
Open
estebank wants to merge 3 commits into
Open
Conversation
When encountering comments in between elements of an or-pattern in a `match` expression, format the matched value, the arm body and the patterns of all other arms. *Only* skip the pattern with embedded comments.
estebank
commented
May 12, 2026
| () => {} | ||
| () // Comment | ||
| | () => { | ||
| println!("Foo"); |
Contributor
Author
There was a problem hiding this comment.
This highlights the outstanding problem with this approach: the pattern is kept with bad formatting, where this should instead have been indented to the proper level.
Contributor
|
Diff-Check failed ❌. We probably have to gate this change on |
Contributor
|
@rustbot author |
Collaborator
|
Reminder, once the PR becomes ready for a review, use |
Contributor
Author
estebank
added a commit
to estebank/rust
that referenced
this pull request
May 13, 2026
Using rust-lang/rustfmt#6893, reformat the codebase. The result is that matches that *would have* been formatted under normal circumstances get their expected format. These match expressions were being entirely skipped because they contain or-patterns with comments in between patterns, causing rustfmt to bail out entirely. The or-patterns with comments themselves remain untouched, but now the match arm bodies and other patterns without comments do get formatted under that PR. Because the fix in rustfmt isn't landed yet, I reworked some of the or-patterns with comments so that formatting doesn't regress. Tried doing this only in larger blocks that are more likely to regress in the meantime.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When encountering comments in between elements of an or-pattern in a
matchexpression, format the matched value, the arm body and the patterns of all other arms. Only skip the pattern with embedded comments.Fix #4119, fix #6491, cc #6044, cc #6663.
An alternative approach would be to make pre and post comments part of all patterns, which would allow us to keep them around and format or patterns properly, but wanted to have the minimal possible change that could address a common papercut I've noticed in
rustc.