rust-lang / rust-lang/rust-clippy
Replace `if let tuple = tuple` by a let chain
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
What it does
Replaces if let (Some(x), Ok(y)) = (x, y) by if let Some(x) = x && Ok(y) = y, i.e. when a tuple is constructed and then immediately matched against a destructuring pattern. Similarly for arrays.
Things like if let (Some(x), Some(y) = (y, x), albeit questionable, should not trigger the lint.
Advantage
- Separates independent checks
- Can short-circuit
Drawbacks
- Can short circuit
- With default formatting, can increase the number of lines
Example
if let (Some(x), Ok(y)) = (x, y) && let [Some(_), None] = [a, b] {
...
}
Could be written as:
if let Some(x) = x && let Ok(y) = y && let Some(_) = a && let None = b {
...
}
Comparison with existing lints
It seems likely that redundant_pattern_match can trigger as a follow-up.
Not a lint, but rust-analyzer's unwrap_tuple assist is the analog for assignments. Note that for assignments, (x, y) = (y, x); is entirely reasonable and idiomatic,
Additional Context
This pattern may have been used as a partial workaround before let chains were available.
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.
Research direction
Start by reviewing Clippy's existing redundant_pattern_match lint and the rust-analyzer unwrap_tuple assist mentioned in the issue. Trace how tuple and array destructuring patterns are recognized, then verify that the proposed let-chain transformation triggers only for immediately constructed values and leaves questionable reordered matches unchanged.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100