Skip to content

Add matches_with_unrelated_if lint#16870

Open
Souradip121 wants to merge 3 commits intorust-lang:masterfrom
Souradip121:master
Open

Add matches_with_unrelated_if lint#16870
Souradip121 wants to merge 3 commits intorust-lang:masterfrom
Souradip121:master

Conversation

@Souradip121
Copy link
Copy Markdown
Contributor

@Souradip121 Souradip121 commented Apr 16, 2026

Fixes #16719

What this PR does

Adds a new style lint matches_with_unrelated_if that warns when
matches!(expr, Pattern if guard) is used and the guard does not
reference any variable introduced by Pattern.

Motivation

The if guard inside matches! implies a relationship between the
pattern bindings and the condition. When the guard is independent of the
pattern, the grouping is misleading. Moving it out makes the intent clearer:

// Before (misleading — `f.is_bar()` has nothing to do with `Some(_)`)
let _ = matches!(f.foo(), Some(_) if f.is_bar());

// After (the two checks are clearly independent)
let _ = matches!(f.foo(), Some(_)) && f.is_bar();

This also helps catch cases where the user intended to use a pattern
binding in the guard but made an oversight (e.g. typed f.is_bar()
instead of x.is_bar() when the pattern was Some(x)).

Implementation

  • New file: clippy_lints/src/matches/matches_with_unrelated_if.rs
  • Registered in declared_lints.rs and matches/mod.rs
  • Category: style, machine-applicable suggestion

Tests

Added tests/ui/matches_with_unrelated_if.rs covering:

  • Guard uses an outer variable (should lint)
  • Pattern has bindings that are never used in the guard (should lint)
  • Pattern has no bindings, guard uses outer variable (should lint)
  • Guard correctly uses the pattern binding (no lint)
  • No guard at all (no lint)
  • Guard uses both a binding and an outer variable (no lint)

changelog: new lint [matches_with_unrelated_if]

@rustbot rustbot added needs-fcp PRs that add, remove, or rename lints and need an FCP S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Apr 16, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 16, 2026

r? @Jarcho

rustbot has assigned @Jarcho.
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

This comment has been minimized.

Warns when matches!(expr, Pattern if guard) is used and the guard does
not reference any variable introduced by the pattern. The guard is
unrelated to the match and belongs outside as matches!(expr, Pattern) && guard.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-fcp PRs that add, remove, or rename lints and need an FCP 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.

New lint idea: matches_with_unrelated_if

3 participants