rust-lang / rust-lang/rust-clippy

Needless call of to_vec() on temporary Vec

Open
#7,908 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-enhancement
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

What it does

When to_vec() is called on temporary Vec<_> the lint should warn that to_vec() is not needed and causes extra allocation. (Suggesting removing it.)

Categories
  • Kind: performance

What is the advantage of the recommended code over the original code

  • One less allocation
  • More concise code (the returned value is already a Vec<_>)
Drawbacks

None.

Example

(playground)

fn create_vec() -> Vec<u8> {
    vec![1, 2, 42]
}

fn main() {
    println!("{:?}", create_vec().to_vec());
}

Could be written as:

fn create_vec() -> Vec<u8> {
    vec![1, 2, 42]
}

fn main() {
    println!("{:?}", create_vec());
}

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 with the Rust examples in the issue and inspect Clippy's existing lint and test conventions for method calls. Confirm the temporary Vec case and define tests covering the suggested removal of to_vec(); done means the lint detects the unnecessary allocation without flagging necessary calls.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
performance, tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.