rust-lang / rust-lang/rust-clippy

Warn on vec.clone().some_vec_method()

Open
#7,312 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

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

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 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.