rust-lang / rust-lang/rust-clippy
New lint idea: `matches_with_unrelated_if`
Open
@Souradip121 is already working on this.
Since Apr 16, 2026.
A-lint
E-medium
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
What it does
Warns against code where the if inside matches! doesn't use any of the variables introduced by the pattern, and suggest moving the if out of the matches!
Advantage
- Removes the illusory connection between the pattern-matching and the if-check
- If the user did intend to use the variable(s) in the
if, the lint could hint at their oversight. - Makes for less complex
matches!, which rustfmt tends to appreciate
Drawbacks
- Sometimes this grouping might be deliberate?
Example
struct Foo;
impl Foo {
fn foo(&self) -> Option<i32> {
todo!()
}
fn is_bar(&self) -> bool {
todo!()
}
}
fn main() {
let f = Foo;
let _ = matches!(f.foo(), Some(_) if l.is_bar());
}
Could be written as:
struct Foo;
impl Foo {
fn foo(&self) -> Option<i32> {
todo!()
}
fn is_bar(&self) -> bool {
todo!()
}
}
fn main() {
let f = Foo;
let _ = matches!(f.foo(), Some(_)) && l.is_bar();
}
Comparison with existing lints
Could not find any similar lints
Additional Context
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.