Skip to content

lint matchable if let for pattern #9221

Closed
@dswij

Description

@dswij

What it does

Checks for if let for patterns that can be re-written with matches!

This can be an improvement to equatable_if_let lint.

For background see #9102 (comment)

Lint Name

No response

Category

No response

Advantage

Similar to equatable_if_let:

  1. It reads better and has less cognitive load because equality won’t cause binding.
  2. It avoids a Yoda condition.
  3. Equality is a simple bool expression and can be merged with && and || and reuse if blocks

Drawbacks

No response

Example

#![allow(dead_code)]
enum Foo {
    Bar,
    Baz,
}

fn test(foo: Foo) {
    if let Foo::Baz = foo {
        println!("Foo::Baz");
    }
}

can be written as

#![allow(dead_code)]
enum Foo {
    Bar,
    Baz,
}

fn test(foo: Foo) {
    if matches!(foo, Foo::Baz) {
        println!("Foo::Baz");
    }
}

Metadata

Metadata

Assignees

Labels

A-lintArea: New lintsC-enhancementCategory: Enhancement of lints, like adding more cases or adding help messagesgood first issueThese issues are a good way to get started with Clippy

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions