Skip to content

Fix collapsible_match suggesting wrong span when arm block has a label#17437

Open
Halloloid wants to merge 2 commits into
rust-lang:masterfrom
Halloloid:fix-17427
Open

Fix collapsible_match suggesting wrong span when arm block has a label#17437
Halloloid wants to merge 2 commits into
rust-lang:masterfrom
Halloloid:fix-17427

Conversation

@Halloloid

@Halloloid Halloloid commented Jul 20, 2026

Copy link
Copy Markdown

Fixes #17427

collapsible_match was computing the wrong span for the opening brace
of a match arm's block when that block had a label (e.g. 'label: { ... }).
It assumed the first byte of the block's span was always {, which only
holds when there's no label — with a label present, the suggestion ended
up deleting the leading ' of the label instead of the arrow, producing
code that failed to compile ("unclosed delimiter").

This finds the actual { position instead of assuming it's the first byte,
and re-inserts the label text after the => in the suggestion so it isn't
silently dropped.

Added a regression test (issue17427 in tests/ui/collapsible_match_fixable.rs)
covering the labeled-block case, verified via both .stderr and .fixed
(rustfix-applied output actually compiles).

changelog: [collapsible_match]: fix incorrect suggestion span causing a
compile error when the outer match arm's block has a label

@rustbot rustbot added the S-waiting-on-community-reviews Status: This is awaiting for positive reviews from the community before a maintainer is assigned. label Jul 20, 2026
@rustbot

rustbot commented Jul 20, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request, and welcome!

You should hear from one of our reviewers after this PR gets at least 2 reviews from the community.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jul 20, 2026

@DanielEScherzer DanielEScherzer left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Community review - makes sense, but inline question

View changes since this review

Comment thread clippy_lints/src/matches/collapsible_match.rs Outdated
@Halloloid

Copy link
Copy Markdown
Author

@rustbot review

@Halloloid

Copy link
Copy Markdown
Author

Community review - makes sense, but inline question

View changes since this review

Updated — now skips the suggestion entirely if no opening brace is found, instead of falling back to position 0.

Comment on lines +173 to +176
let block_snippet = snippet(cx, block_span, "");
let Some(brace_offset) = block_snippet.find('{') else {
return;
};

@Gri-ffin Gri-ffin Jul 22, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What if I do something like this:

    'label: /* { .... } */ {

I believe the current solution would confuse both since the comment would be included block_snippet.

View changes since the review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-community-reviews Status: This is awaiting for positive reviews from the community before a maintainer is assigned. 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.

collapsible_match suggestion on labeled match arm block causes syntax error

4 participants