rust-lang / rust-lang/rust-clippy
Warn on vec.clone().some_vec_method()
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
What it does
Warn when chaining sort(), dedup(), or other method that changes the vector after to_vec() or clone(). Example:
// Probably an error as sort() returns () not a vec
let mut new_vec = old_vec.clone().sort();
// This is probably what you wanted
let mut new_vec = old_vec.clone()
mut new_vec.sort();
Categories (optional)
- Kind: clippy::correctness
What is the advantage of the recommended code over the original code
I'm not sure.
Drawbacks
The Rust compiler will correctly tell if () uses a vector function later.
Example
let mut new_vec = old_vec.clone().sort();
// This will error, () does not have push() method
new_vec.push(0);
// No error here, () implements Debug (or is it Default? that allows printing below)
println!("New vec: {}", new_vec);
// No error here, () has a clone() method
let cloned_vec = new_vec.clone();
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 requested Rust examples and the existing Clippy lint conventions; no implementation files or tests are named in the issue. Define the supported vector-mutating method chains and their diagnostic behavior, then add coverage showing warnings for the erroneous chains and no warning for the separate mutable-vector form.
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
- 25/100