Skip to content

Don't suggest collapsible_match guard when condition mutates pattern binding#16869

Open
JasmineLCY wants to merge 1 commit intorust-lang:masterfrom
JasmineLCY:fix/collapsible-match-mutable-guard
Open

Don't suggest collapsible_match guard when condition mutates pattern binding#16869
JasmineLCY wants to merge 1 commit intorust-lang:masterfrom
JasmineLCY:fix/collapsible-match-mutable-guard

Conversation

@JasmineLCY
Copy link
Copy Markdown

@JasmineLCY JasmineLCY commented Apr 16, 2026

When mutated_variables returns None (cannot determine mutations), pat_bindings_moved_or_mutated silently ignored the result instead of conservatively bailing out. This caused the lint to suggest match guards for expressions that require mutable access to pattern bindings, which is forbidden in guards — variables are immutable in pattern guards.

The fix changes the if let Some(mutated) to let Some(mutated) ... else { return true }, matching the existing conservative bail-out for ExprUseVisitor errors a few lines above.

Fixes #16864

changelog: Fix [collapsible_match] suggesting a match guard when the condition mutates a pattern binding


Drafted with AI assistance. Reviewed, tested, and verified by me.

…n binding

When `mutated_variables` returns `None` (cannot determine mutations),
`pat_bindings_moved_or_mutated` silently ignored the result instead of
conservatively bailing out. This caused the lint to suggest match guards
for expressions that require mutable access to pattern bindings, which
is forbidden in guards.

Fixes rust-lang#16864
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Apr 16, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 16, 2026

r? @llogiq

rustbot has assigned @llogiq.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: 7 candidates
  • 7 candidates expanded to 7 candidates
  • Random selection from Jarcho, dswij, llogiq, samueltardieu

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 16, 2026

⚠️ Warning ⚠️

  • There are issue links (such as #123) in the commit messages of the following commits.
    Please move them to the PR description, to avoid spamming the issues with references to the commit, and so this bot can automatically canonicalize them to avoid issues with subtree.

@samueltardieu
Copy link
Copy Markdown
Member

Please claim the issue you want to fix so that nobody works on it as well, and remove the issue number from the commit message as indicated in #16869 (comment)

@JasmineLCY
Copy link
Copy Markdown
Author

Apologies for the delayed response. Will claim the issue and fix the commit message.

Copy link
Copy Markdown
Contributor

@llogiq llogiq left a comment

Choose a reason for hiding this comment

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

The code itself looks ok. Please fix the commit message so we can merge.

View changes since this review

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

Labels

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 suggestions for pattern guards can cause compilation errors

4 participants