Skip to content

redundant_pattern_matching: parenthesize guarded matches! suggestion#17287

Open
shulaoda wants to merge 1 commit into
rust-lang:masterfrom
shulaoda:fix/redundant-pattern-matching-guard-parens
Open

redundant_pattern_matching: parenthesize guarded matches! suggestion#17287
shulaoda wants to merge 1 commit into
rust-lang:masterfrom
shulaoda:fix/redundant-pattern-matching-guard-parens

Conversation

@shulaoda

@shulaoda shulaoda commented Jun 21, 2026

Copy link
Copy Markdown
Contributor

The suggestion for a guarded matches!(x, Pat if guard) was built as x.is_some() && guard but never wrapped in parentheses, so applying it where the matches!(...) binds tighter than && (!matches!(...), matches!(...) == false, a method receiver, an as cast, ...) changed operator precedence. That either silently changed the program's behavior or broke compilation.

The fix wraps the whole is_xxx() && guard suggestion in parentheses when the parent expression binds tighter than &&, and leaves it untouched otherwise (statements, if/while conditions, arguments, &&/|| operands).

Fixes #17286

changelog: [redundant_pattern_matching]: parenthesize the suggestion for a guarded matches! so the autofix keeps the original operator precedence

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

rustbot commented Jun 21, 2026

Copy link
Copy Markdown
Collaborator

r? @dswij

rustbot has assigned @dswij.
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: 8 candidates
  • 8 candidates expanded to 8 candidates
  • Random selection from Jarcho, dswij, llogiq, samueltardieu

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.

redundant_pattern_matching: the auto-fix for a guarded matches! drops parentheses and silently changes behavior

3 participants