rust-lang / rust-lang/rust-clippy
catch suspicious clones: let foo = foo.clone(); (to appease borrowck)
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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