rust-lang / rust-lang/rust-clippy
warn when using vec![_; 0] instead of vec![0; _] or vec![]
Open
@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![]norvec![0; x()]is the same asvec![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
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.
Assessment
This issue has not been assessed yet.