rust-lang / rust-lang/rust-clippy

Use `if let` instead of `if matches!`

Open
#16,660 6 comments 0 reactions 1 assignee View on GitHub

@Taym95 is already working on this.

Since Mar 5, 2026.

A-lint
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

What it does

Warn against use of if matches! where it could be written as if let. I don't know in which cases this is or isn't safe.

This should probably be a pedantic lint (see drawbacks).

Advantage
  • More readable
  • Shorter
  • More idiomatic
Drawbacks
  • The pattern and the value are switched around, which might've been done intentionally for readability.
Example
enum E {
    A,
    B,
}

fn main() {
    let x = E::A;
    if matches!(x, E::A) {
        println!("It's an A");
    }
}

Could be written as:

enum E {
    A,
    B,
}

fn main() {
    let x = E::A;
    if let E::A = x {
        println!("It's an A");
    }
}

And:

enum E {
    A(u32),
    B,
}

fn main() {
    let x = E::A(1);
    if matches!(x, E::A(n) if n == 1) {
        println!("It's an A(1)");
    }
}

Could be written as:

enum E {
    A(u32),
    B,
}

fn main() {
    let x = E::A(1);
    if let E::A(n) = x && n == 1 {
        println!("It's an A(1)");
    }
}

(As long as let chains are available in the current Rust version)

Comparison with existing lints

No response

Additional Context

No response

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.