Clippy lints / Nursery
Clippy lint · Nursery · checked with Clippy 0.1.99
clippy::collection_is_never_read: Checks for collections that are never queried
What it meansChecks for collections that are never queried. It is in the nursery group and off by default.
Does it matter?
Putting effort into constructing a collection but then never querying it might indicate that the author forgot to do whatever they intended to do with the collection. Example: Clone a vector, sort it for iteration, but then mistakenly iterate the original vector instead.
New lints still being finished. Off by default and may have false positives.
How to fix it
Clippy does not fix this one automatically. Change the code by hand:
Flagged
let mut sorted_samples = samples.clone();
sorted_samples.sort();
for sample in &samples { // Oops, meant to use `sorted_samples`.
println!("{sample}");
}
Write this instead
let mut sorted_samples = samples.clone();
sorted_samples.sort();
for sample in &sorted_samples {
println!("{sample}");
}
Turn it off, or on
It is off by default. To use it, set it to warn or deny:
Whole crate (Cargo.toml)
[lints.clippy]
collection_is_never_read = "warn"In code
#![warn(clippy::collection_is_never_read)]One run
cargo clippy -- -W clippy::collection-is-never-readThe [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 collections that are never queried.
Why is this bad?
Putting effort into constructing a collection but then never querying it might indicate that the author forgot to do whatever they intended to do with the collection. Example: Clone a vector, sort it for iteration, but then mistakenly iterate the original vector instead.
Example
let mut sorted_samples = samples.clone();
sorted_samples.sort();
for sample in &samples { // Oops, meant to use `sorted_samples`.
println!("{sample}");
}
Use instead:
let mut sorted_samples = samples.clone();
sorted_samples.sort();
for sample in &sorted_samples {
println!("{sample}");
}
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- redundant_cloneChecks for a redundant clone() (and its relatives) which clones an owned value that is going to be dropped without further use
- branches_sharing_codeChecks if the blocks of an if/else, or the arms of a match, contain shared code that can be moved out of the branches
- clear_with_drain`drain` used to clear a `Vec`
- coerce_container_to_anyProtects against unintended coercion of references to container types to &dyn Any when the container type dereferences to a dyn Any which could be directly referenced instead
- debug_assert_with_mut_callChecks for function/method calls with a mutable parameter in debug_assert!, debug_assert_eq! and debug_assert_ne! macros
- derive_partial_eq_without_eqChecks for types that derive PartialEq and could implement Eq
- doc_link_codecode link adjacent to code text
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::redundant_cloneChecks for a redundant clone() (and its relatives) which clones an owned value that is going to be dropped without further useRead it next →Did this answer your question?