rust-lang / rust-lang/rust-clippy
Don't pass `vec![]` to `IntoIterator` arguments when not needed
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
What it does
A method accepting IntoIterator does not need to get a vec![13, 37] and can take a [13, 37] instead.
Advantage
When looking at it with cargo-show-asm Rust seems to optimize away the allocations of vec![] but it still results in more instructions than without it. Using black_box results in the allocations happening which is a lot more instructions than the version with a [T; N].
I assume that in more complex examples these optimizations might not always work resulting in more instructions / allocations happening at runtime. As it results in less instructions with optimizations too it's always a benefit of preferring [T; N] over vec![].
It's also more concise source code.
Drawbacks
requires Rust 1.53
Example
pub fn method<I>(items: I)
where
I: IntoIterator<Item = u8>,
{
for entry in items {
black_box(entry);
}
}
method(vec![13, 37]);
Could be written as:
method([13, 37]);
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.
Research direction
Start from the proposed Rust examples and the IntoIterator requirement, then review how Clippy implements comparable lints and handles the Rust 1.53 compatibility constraint. Define the lint's applicable cases and verify that replacing vec![...] with an array preserves the intended iteration behavior and avoids the reported allocation or instruction overhead.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100