Skip to content

Format match exprs even when or-patterns have comments#6893

Open
estebank wants to merge 3 commits into
rust-lang:mainfrom
estebank:comment-in-pattern
Open

Format match exprs even when or-patterns have comments#6893
estebank wants to merge 3 commits into
rust-lang:mainfrom
estebank:comment-in-pattern

Conversation

@estebank
Copy link
Copy Markdown
Contributor

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.

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.

estebank added 2 commits May 12, 2026 03:16
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.
@rustbot rustbot added the S-waiting-on-review Status: awaiting review from the assignee but also interested parties. label May 12, 2026
Comment thread tests/target/match.rs
() => {}
() // Comment
| () => {
println!("Foo");
Copy link
Copy Markdown
Contributor Author

@estebank estebank May 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

View changes since the review

@ytmimi
Copy link
Copy Markdown
Contributor

ytmimi commented May 12, 2026

Diff-Check failed ❌. We probably have to gate this change on style_edition=2027.

@ytmimi ytmimi added the F-impacts-stable-unformatted-code Expected formatting impact: affects stable + unformatted code (caution) label May 12, 2026
@ytmimi
Copy link
Copy Markdown
Contributor

ytmimi commented May 12, 2026

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: awaiting review from the assignee but also interested parties. labels May 12, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented May 12, 2026

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@rustbot rustbot added the A-CI Area: CI label May 13, 2026
@estebank
Copy link
Copy Markdown
Contributor Author

@rustbot ready

@ytmimi I added (not yet existing) Edition 2027 and gated the new behavior behind it. Is that the usual modus operandi in rustfmt when introducing behavior changes that have big fan-out potential? I think the big diff in CI goes to show just how prevalent a gap this is :)

@rustbot rustbot added S-waiting-on-review Status: awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: awaiting some action (such as code changes or more information) from the author. labels May 13, 2026
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-CI Area: CI F-impacts-stable-unformatted-code Expected formatting impact: affects stable + unformatted code (caution) S-waiting-on-review Status: awaiting review from the assignee but also interested parties.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Formatting does not work with match arm having mixed OR clauses and comments silently fails to format match expression if arm pat contains comment

3 participants