rust-lang / rust-lang/rust-clippy

Replace `if let tuple = tuple` by a let chain

Open
#17,169 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint
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

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.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.