rust-lang / rust-lang/rust-clippy

New lint: collapsible `matches!`-es

Open
#15,435 0 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 multiple matches!() on the same variable with one that has all the patterns combined

Advantage
  • More concise
  • (Potentially) less work for the optimizer
Drawbacks

No response

Example
enum Foo {
    Bar, Baz
}
let f = Foo::Bar;
let _ = matches!(f, Foo::Bar) || matches!(f, Foo::Baz);

Could be written as:

/* snip */
let _ = matches!(f, Foo::Bar | Foo::Baz);
Comparison with existing lints

no similar lints

Additional Context

seen at https://github.com/rust-lang/rust-clippy/blob/6f2567d16da654e38404c93307594a04a03deddb/clippy_lints/src/floating_point_arithmetic.rs#L495

would be a bit difficult to implement since one'd need to look at the pre-expansion source

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 reading clippy_lints/src/floating_point_arithmetic.rs around line 495 and trace how the existing related pattern is recognized. Investigate how Clippy accesses pre-expansion source, then define the lint behavior for multiple matches! calls on one variable, including the combined-pattern example; done means the proposed case is detected without false matches.

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
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.