rust-lang / rust-lang/rust-clippy

Also check `match`es for `branches_sharing_code`

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

Description

Example (albeit contrived):

fn main() {
    let s = "RustAnalyzer";
    let mut s2 = String::new();
    for (i, c) in s.chars().enumerate() {
        let mut c2 = c;
        match c {
            'A'..='Z' => {
                if i != 0 {
                    s2.push('-');
                }
                c2 = c.to_ascii_lowercase();
                s2.push(c2);
            }
            _ => {
                s2.push(c2);
            }
        }
    }
    println!("{s2}");
}

Could be written as:

fn main() {
    let s = "RustAnalyzer";
    let mut s2 = String::new();
    for (i, c) in s.chars().enumerate() {
        let mut c2 = c;
        match c {
            'A'..='Z' => {
                if i != 0 {
                    s2.push('-');
                }
                c2 = c.to_ascii_lowercase();
            }
            _ => {}
        }
        s2.push(c2);
    }
    println!("{s2}");
}

(which in this case could get further reduced by single_match)

Version
rustc 1.87.0-nightly (f280acf4c 2025-02-19)
binary: rustc
commit-hash: f280acf4c743806abbbbcfe65050ac52ec4bdec0
commit-date: 2025-02-19
host: x86_64-pc-windows-msvc
release: 1.87.0-nightly
LLVM version: 20.1.0
Additional Labels

No response

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 locating the implementation and tests for the branches_sharing_code lint in rust-clippy. Compare its current handling of conditional branches with the match example, then add coverage showing the intended diagnostic behavior and run the relevant lint tests to verify it.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.