rust-lang / rust-lang/rust-clippy

catch suspicious clones: let foo = foo.clone(); (to appease borrowck)

Open
#5,904 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Inspired by this change: https://github.com/rust-lang/rust-clippy/pull/5897/files

It might be interesting to catch occurrences of let foo = foo.clone() in order to make code work where we get a reference of foo &foo but need to pass it to a function that consumes foo and .clone() it to appease the borrow checker:

 #[derive(Clone)]
struct A {}

fn main() {
    let a: A = A {};
    no_consume(&a);
}

fn consume(a: A) {}

fn no_consume(a: &A) {
    let a = a.clone();
    consume(a);
}

IMO code like this is worth looking at to see if we can avoid the clone by not passing &A by reference:

fn main() {
    let a: A = A {};
    do_consume(a);
}

fn consume(a: A) {}

fn do_consume(a: A) {
    consume(a);
}

There might be a lot of false positives so this would probably be a pedantic lint.
The lint should probably not fire for let mut foo = foo.clone() as well?

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 linked rust-clippy pull request and the Rust examples in this issue. Define the lint's behavior, including false-positive handling and whether let mut foo = foo.clone() is excluded; done means the proposed cases and these edge cases have an agreed, testable outcome.

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.