Skip to content

Replace if let tuple = tuple by a let chain #17169

@ronnodas

Description

@ronnodas

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lints

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions