Clippy lints / Suspicious
Clippy lint · Suspicious · checked with Clippy 0.1.99
clippy::single_range_in_vec_init: an array of `Range` that is only one element
What it meansChecks for Vec or array initializations that contain only one range. It is in the suspicious group and warns by default.
What you see
warning: an array of `Range` that is only one element --> main.rs:3:9 | 3 | let x = [0..200]; | ^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#single_range_in_vec_init = note: `#[warn(clippy::single_range_in_vec_init)]` on by default help: if you wanted a `Vec` that contains the entire range, try | 3 - let x = [0..200]; 3 + let x = (0..200).collect::<std::vec::Vec<i32>>(); | help: if you wanted an array of len 200, try | 3 - let x = [0..200]; 3 + let x = [0; 200]; |
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 is almost always incorrect, as it will result in a Vec that has only one element. Almost always, the programmer intended for it to include all elements in the range or for the end of the range to be the length instead.
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 x = [0..200];
Write this instead
// If it was intended to include every element in the range...
let x = (0..200).collect::<Vec<i32>>();
// ...Or if 200 was meant to be the len
let x = [0; 200];
Turn it off, or on
Allow it where it does not fit your code, as narrowly as you can:
On one item
#[allow(clippy::single_range_in_vec_init)]
fn my_function() { /* ... */ }Whole crate (Cargo.toml)
[lints.clippy]
single_range_in_vec_init = "allow"One run
cargo clippy -- -A clippy::single-range-in-vec-initThe [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
Checks for Vec or array initializations that contain only one range.
Why is this bad?
This is almost always incorrect, as it will result in a Vec that has only one element.
Almost always, the programmer intended for it to include all elements in the range or for
the end of the range to be the length instead.
Example
let x = [0..200];
Use instead:
// If it was intended to include every element in the range...
let x = (0..200).collect::<Vec<i32>>();
// ...Or if 200 was meant to be the len
let x = [0; 200];
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- redundant_localsredundant redefinition of a binding `a`
- repeat_vec_with_capacityLooks for patterns such as vec![Vec::with_capacity(x); n] or iter::repeat(Vec::with_capacity(x))
- repr_packed_without_abiitem uses `packed` representation without ABI-qualification
- size_of_refargument to `size_of_val()` is a reference to a reference
- suspicious_arithmetic_implLints for suspicious operations in impls of arithmetic operators, e.g. subtracting elements in an Add impl
- suspicious_assignment_formattingthis looks like you are trying to use `.. -= ..`, but you really are doing `.. = (- ..)`
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::redundant_localsredundant redefinition of a binding `a`Read it next →Did this answer your question?