Skip to content

New lint: collapsible_guards #7825

@camsteffen

Description

@camsteffen

What it does

Detects match guards that can be collapsed into the pattern.

Categories (optional)

  • Kind: complexity

What is the advantage of the recommended code over the original code

It is simpler.

Drawbacks

None.

Example

match x {
    Some(x) if matches!(x, Some(1)) => .., // if matches!
    Some(x) if x == Some(2) => .., // if ==
    x => matches!(x, Some(x) if x == 3), // guard inside matches!
}

Could be written as:

match x {
    Some(Some(1)) => ..,
    Some(Some(2)) => ..,
    x => matches!(x, Some(3)),
}

This lint can share some logic with equatable_if_let. @HKalbasi maybe you'd like to take this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: New lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions