Clippy lints / Style
Clippy lint · Style · checked with Clippy 0.1.99
clippy::same_item_push: it looks like the same item is being pushed into this `Vec`
What it meansChecks whether a for loop is being used to push a constant value into a Vec. It is in the style group and warns by default.
What you see
warning: it looks like the same item is being pushed into this `Vec` --> main.rs:7:5 | 7 | vec.push(item1); | ^^^ | = help: consider using `vec![item1;SIZE]` = help: or `vec.extend(std::iter::repeat_n(item1, SIZE))` = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#same_item_push = note: `#[warn(clippy::same_item_push)]` 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?
This kind of operation can be expressed more succinctly with vec![item; SIZE] or vec.resize(NEW_SIZE, item) and using these alternatives may also have better performance.
Code that works but is not idiomatic Rust. Warns by default.
How to fix it
Clippy does not fix this one automatically. Change the code by hand:
Flagged
let item1 = 2;
let item2 = 3;
let mut vec: Vec<u8> = Vec::new();
for _ in 0..20 {
vec.push(item1);
}
for _ in 0..30 {
vec.push(item2);
}
Write this instead
let item1 = 2;
let item2 = 3;
let mut vec: Vec<u8> = vec![item1; 20];
vec.resize(20 + 30, item2);
Turn it off, or on
Allow it where it does not fit your code, as narrowly as you can:
On one item
#[allow(clippy::same_item_push)]
fn my_function() { /* ... */ }Whole crate (Cargo.toml)
[lints.clippy]
same_item_push = "allow"One run
cargo clippy -- -A clippy::same-item-pushThe [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
Checks whether a for loop is being used to push a constant value into a Vec.
Why is this bad?
This kind of operation can be expressed more succinctly with
vec![item; SIZE] or vec.resize(NEW_SIZE, item) and using these alternatives may also
have better performance.
Example
let item1 = 2;
let item2 = 3;
let mut vec: Vec<u8> = Vec::new();
for _ in 0..20 {
vec.push(item1);
}
for _ in 0..30 {
vec.push(item2);
}
Use instead:
let item1 = 2;
let item2 = 3;
let mut vec: Vec<u8> = vec![item1; 20];
vec.resize(20 + 30, item2);
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
- needless_returnunneeded `return` statement
- new_without_defaultChecks for public types with a pub fn new() -> Self method and no implementation of Default
- len_zeroChecks for getting the length of something via .len() just to compare to zero, and suggests using .is_empty() where applicable
- redundant_static_lifetimesconstants have by default a `'static` lifetime
- result_map_or_into_optionChecks for usage of _.map_or(None, Some)
- result_unit_errthis returns a `Result<_, ()>`
- self_named_constructorsconstructor `foo` has the same name as the type
- should_implement_traitChecks for methods that should live in a trait implementation of a std trait (see llogiq’s blog post for further information) instead of an inherent implementation
- single_char_add_strWarns when using push_str/insert_str with a single-character string literal where push/insert with a char would work fine
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::needless_returnunneeded `return` statementRead it next →Did this answer your question?