Clippy lints / Complexity
Clippy lint · Complexity · checked with Clippy 0.1.99
clippy::reserve_after_initialization: call to `reserve` immediately after creation
What it meansInforms the user about a more concise way to create a vector with a known capacity. It is in the complexity group and warns by default.
What you see
warning: call to `reserve` immediately after creation --> main.rs:3:1 | 3 | / let mut v: Vec<usize> = vec![]; 4 | | v.reserve(10); | |______________^ help: consider using `Vec::with_capacity(/* Space hint */)`: `let mut v: Vec<usize> = Vec::with_capacity(10);` | = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#reserve_after_initialization = note: `#[warn(clippy::reserve_after_initialization)]` on by default
Real output of Clippy 0.1.99 (Rust 1.99) on the example below. Names and line numbers in yours will differ.
Does it matter?
The Vec::with_capacity constructor is less complex.
Code that does something simple in a complicated way. 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 mut v: Vec<usize> = vec![];
v.reserve(10);
Write this instead
let mut v: Vec<usize> = Vec::with_capacity(10);
Turn it off, or on
Allow it where it does not fit your code, as narrowly as you can:
On one item
#[allow(clippy::reserve_after_initialization)]
fn my_function() { /* ... */ }Whole crate (Cargo.toml)
[lints.clippy]
reserve_after_initialization = "allow"One run
cargo clippy -- -A clippy::reserve-after-initializationThe [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.
Clippy's own documentation
What it does
Informs the user about a more concise way to create a vector with a known capacity.
Why is this bad?
The Vec::with_capacity constructor is less complex.
Example
let mut v: Vec<usize> = vec![];
v.reserve(10);
Use instead:
let mut v: Vec<usize> = Vec::with_capacity(10);
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- too_many_argumentsChecks for functions with too many parameters
- clone_on_copyusing `clone` on type `u64` which implements the `Copy` trait
- type_complexityChecks for types used in structs, parameters and let declarations above a certain complexity threshold
- redundant_guardsChecks for unnecessary guards in match expressions
- redundant_slicingredundant slicing of the whole range
- repeat_oncecalling `repeat(1)` on a string literal
- result_filter_map`filter` for `Ok` followed by `unwrap`
- result_map_unit_fnChecks for usage of result.map(f) where f is a function or closure that returns the unit type ()
- seek_from_currentusing `SeekFrom::Current` to start from current position
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::too_many_argumentsChecks for functions with too many parametersRead it next →Did this answer your question?