rust-lang / rust-lang/rust-clippy
Use `if let` instead of `if matches!`
Open
@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
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.