rust-lang / rust-lang/rust-clippy

New lint: `duplicate_match_guards`

Open
#14,971 2 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

Checks for the same condition being checked in a match guard and in the match body

Advantage
  • Remove a likely typo or a copy and paste error
Drawbacks
  • False negatives: if the condition is an impure function, it could've been called twice on purpose for its side effects
  • removing a match guard might change the matching logic (so it might make sense to only suggest removing the inner if)
Example
match n {
    0 if a > b => {
        if a > b {
            return;
        }
    }
    _ => {}
}

Could be written as:

match n {
    0 if a > b => {
        return;
    }
    _ => {}
}

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 the issue's Rust match example and the proposed duplicate condition check. Resolve whether the lint should remove the inner condition or the match guard, accounting for impure functions and matching semantics. Done means the lint behavior and its handling of these tradeoffs are clearly specified.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.