rust-lang / rust-lang/rust-clippy

New lint: Repetitive branches

Open
#5,801 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-lint E-medium L-complexity L-suggestion
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

What it does

Identifies code where each branch performs the same transformation on some value.

Categories (optional)
  • Kind: clippy::style?

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

It is less repetitive and often creates opportunities for further simplification.

Drawbacks

In some cases, the benefit may be negligible or may differ from stylistic preference.

Example
match n {
    1 => Ok("one")
    2 => Ok("two")
    3 => {
        // suppose there is more code here
        Ok("three")
    }
    _ => Ok("other")
}

Could be written as:

let s = match n {
    1 => "one"
    2 => "two"
    3 => {
        // suppose there is more code here
        "three"
    }
    _ => "other"
};
Ok(s)

This lint can apply to several constructs: match, if/else if/else, if let

The transformation could be a wrapper struct or enum or a function call.

The variable value should be a single value. Not, for example, a list of function arguments.

I'm not sure if this should be pedantic. Personally, I would probably want to #[warn] it.

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 proposed match and conditional examples in the issue, then determine how the lint should recognize a single transformed value across match, if/else, and if let constructs. Define the accepted transformation shapes and whether the lint belongs in the default or pedantic category. Done means the behavior and diagnostics are specified well enough to implement and test consistently.

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
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.