rust-lang / rust-lang/rust-clippy

warn when using vec![_; 0] instead of vec![0; _] or vec![]

Open
#16,430 1 comment 0 reactions 1 assignee View on GitHub

@skatromb is already working on this.

Since Mar 8, 2026.

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 creating an empty Vec using the vec![] macro with an unnecessary initialization value

Advantage

I'm pretty sure if someone writes vec![123; 0] it's because they mixed up the parameters

Creating a zero filled Vec with a certain length is a common operation, on the other hand afaik there's no reason someone would want to do vec![x; 0] instead of vec![]

Drawbacks
  • This won't help users who use other initializations values like vec![1024; 255], but still 0 may be the most common case
  • I can't think of any good auto fix since neither vec![] nor vec![0; x()] is the same as vec![x(); 0]
Example
let buffer = vec![1024; 0];

Could be written as:

let buffer = vec![];

Or if you wanted to create a zero filled Vec:

let buffer = vec![0; 1024];
Comparison with existing lints

No response

Additional Context

I can implement this myself if you guys agree

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.