rust-lang / rust-lang/rust-clippy
Needless call of to_vec() on temporary Vec
Open
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
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
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 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