rust-lang / rust-lang/rust-clippy

Copy-paste detector of the source code

Open
#9,869 1 comment 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

I think it is a good idea to traverse the AST parsed from the source code and try looking for duplicates with some degree of similarity (or rather a full copy) from another place. I have recently had a PR made into one of my projects which had precisely the same copied from a hundred lines of code above, even with the doc comments. If I hadn't been reviewing it well and thoroughly, I'd have missed it. I think it is a good idea to automate this and have such checks done in CI, and I think it is possible to do. Besides all, there are other projects already doing almost that, for example, jscpd or cpd.

Lint Name

copy-paste

Category

correctness, suspicious, style

Advantage
  • Remove the duplicate code either in parts or full code branches (functions, methods, impls, comments, parts of a function body).
  • Increase the code quality and readability.
  • Possibly allow for early refactoring and/or optimisations made by the copy-paste elimination.
Drawbacks
  • Might be comparatively difficult or performance/time-wise costly to find duplicates.
  • Full duplicates are rare but would be helpful to reduce; however, partial code duplication might be linted false-positively. In this case, it would require a lint suppression (if it was enabled for the project).
Example
struct A;
#[cfg(target_os = "linux")]
impl A {
    fn do(&self) {
        println!("Hello world!");
    }

    fn linux_specific_function(&self) {
        println!("Hello from Linux!");
    }
}

#[cfg(target_os = "windows")]
impl A {
    fn do(&self) {
        println!("Hello world!");
    }

    fn windows_specific_function(&self) {
        println!("Hello from Windows!");
    }
}

Could be written as:

struct A;

impl A {
    fn do(&self) {
        println!("Hello world!");
    }
}

#[cfg(target_os = "linux")]
impl A {
    fn linux_specific_function(&self) {
        println!("Hello from Linux!");
    }
}

#[cfg(target_os = "windows")]
impl A {
    fn windows_specific_function(&self) {
        println!("Hello from Windows!");
    }
}

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

No source files, tests, or entry points are named. Start by reviewing the proposed AST traversal, similarity detection, CI integration, and false-positive concerns, then inspect the existing Clippy lint structure. Done would require a defined scope and an accepted implementation plan for the copy-paste lint.

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
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.