rust-lang / rust-lang/rust-clippy

New lint idea: `matches_with_unrelated_if`

Open
#16,719 1 comment 2 reactions 1 assignee View on GitHub

@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

Inspired by https://github.com/rust-lang/rust-analyzer/blob/c6b7da6e5d36bea554ab16db2d1be700ef79cf97/crates/ide-completion/src/context/analysis.rs#L947-L950

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.