Clippy lints / Suspicious
Clippy lint · Suspicious · checked with Clippy 0.1.99
clippy::repeat_vec_with_capacity: Looks for patterns such as vec![Vec::with_capacity(x); n] or iter::repeat(Vec::with_capacity(x))
What it meansLooks for patterns such as vec![Vec::with_capacity(x); n] or iter::repeat(Vec::with_capacity(x)). It is in the suspicious group and warns by default.
Does it matter?
These constructs work by cloning the element, but cloning a Vec<_> does not respect the old vector’s capacity and effectively discards it. This makes iter::repeat(Vec::with_capacity(x)) especially suspicious because the user most certainly expected that the yielded Vec<_> will have the requested capacity, otherwise one can simply write iter::repeat(Vec::new()) instead and it will have the same effect. Similarly for vec![x; n], the element x is cloned to fill the vec. Unlike iter::repeat however, the vec repeat macro does not have to clone the value n times but just n - 1 times, because it can reuse the passed value for the last slot. That means that the last Vec<_> gets the requested capacity but all other ones do not.
Code that is most likely wrong or useless. Warns by default.
How to fix it
Clippy does not fix this one automatically (its suggestion may change behaviour, so it only shows it). Change the code by hand:
Flagged
let _: Vec<Vec<u8>> = vec![Vec::with_capacity(42); 123];
let _: Vec<Vec<u8>> = iter::repeat(Vec::with_capacity(42)).take(123).collect();
Write this instead
let _: Vec<Vec<u8>> = iter::repeat_with(|| Vec::with_capacity(42)).take(123).collect();
// ^^^ this closure executes 123 times
// and the vecs will have the expected capacity
Turn it off, or on
Allow it where it does not fit your code, as narrowly as you can:
On one item
#[allow(clippy::repeat_vec_with_capacity)]
fn my_function() { /* ... */ }Whole crate (Cargo.toml)
[lints.clippy]
repeat_vec_with_capacity = "allow"One run
cargo clippy -- -A clippy::repeat-vec-with-capacityThe [lints] table needs Rust 1.74 or newer. In a workspace, put it under [workspace.lints.clippy] and add [lints] workspace = true to each member.
Settings in clippy.toml
msrv: The minimum rust version that the project supports. Defaults to the rust-version field in Cargo.toml
Clippy's own documentation
What it does
Looks for patterns such as vec![Vec::with_capacity(x); n] or iter::repeat(Vec::with_capacity(x)).
Why is this bad?
These constructs work by cloning the element, but cloning a Vec<_> does not
respect the old vector’s capacity and effectively discards it.
This makes iter::repeat(Vec::with_capacity(x)) especially suspicious because the user most certainly
expected that the yielded Vec<_> will have the requested capacity, otherwise one can simply write
iter::repeat(Vec::new()) instead and it will have the same effect.
Similarly for vec![x; n], the element x is cloned to fill the vec.
Unlike iter::repeat however, the vec repeat macro does not have to clone the value n times
but just n - 1 times, because it can reuse the passed value for the last slot.
That means that the last Vec<_> gets the requested capacity but all other ones do not.
Example
let _: Vec<Vec<u8>> = vec![Vec::with_capacity(42); 123];
let _: Vec<Vec<u8>> = iter::repeat(Vec::with_capacity(42)).take(123).collect();
Use instead:
let _: Vec<Vec<u8>> = iter::repeat_with(|| Vec::with_capacity(42)).take(123).collect();
// ^^^ this closure executes 123 times
// and the vecs will have the expected capacity
Configuration
-
msrv: The minimum rust version that the project supports. Defaults to therust-versionfield inCargo.toml(default:
current version)
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- print_in_format_impluse of `println!` in `Display` impl
- rc_clone_in_vec_initinitializing a reference-counted pointer in `vec![elem; len]`
- redundant_localsredundant redefinition of a binding `a`
- repr_packed_without_abiitem uses `packed` representation without ABI-qualification
- single_range_in_vec_initan array of `Range` that is only one element
- size_of_refargument to `size_of_val()` is a reference to a reference
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::print_in_format_impluse of `println!` in `Display` implRead it next →Did this answer your question?