rust-lang / rust-lang/rust-clippy

`needless_vec`: `Vec::append` or `Vec::extend` using `vec!`.

Open
#13,461 2 comments 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

Detects code that uses Vec::append or Vec::extend with a newly-created vec!.

Advantage
  • Removes unnecessary allocations
  • Clearer intent with push instead of append/extend
Drawbacks

Should there be a separate lint for some_vec.extend([single_elem]) to use push instead? This lint could create code that would require linting again.

Example
some_vec.append(&mut vec![elem]); // 1
some_vec.extend(vec![elem]); // 2
some_vec.append(&mut vec![elem1, elem2]); // 3
some_vec.extend(vec![elem1, elem2]); // 4
// 5:
let mut v = vec![elem1, elem2];
some_vec.append(&mut v);

Could be written as:

some_vec.push(elem); // 1 and 2
some_vec.extend([elem1, elem2]); // 3 and 4
// 5
let v = [elem1, elem2];
some_vec.extend(v);

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 proposed Vec::append and Vec::extend patterns in the issue examples and determine the intended lint scope, including whether single-element arrays need separate handling. Resolve the question about lint interactions before implementing; done means the accepted cases and exclusions are clearly defined and covered by appropriate Clippy tests.

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
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.