Clippy lints / Complexity
Clippy lint · Complexity · checked with Clippy 0.1.99
clippy::inspect_for_each: called `inspect(..).for_each(..)` on an `Iterator`
What it meansChecks for usage of inspect().for_each(). It is in the complexity group and warns by default.
What you see
warning: called `inspect(..).for_each(..)` on an `Iterator` --> main.rs:4:2 | 4 | .inspect(|&x| println!("inspect the number: {}", x)) | __^ 5 | | .for_each(|&x| { 6 | | assert!(x >= 0); 7 | | }); | |__^ | = help: move the code from `inspect(..)` to `for_each(..)` and remove the `inspect(..)` = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#inspect_for_each = note: `#[warn(clippy::inspect_for_each)]` 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?
It is the same as performing the computation inside inspect at the beginning of the closure in for_each.
Code that does something simple in a complicated way. Warns by default.
How to fix it
Clippy does not fix this one automatically. Change the code by hand:
Flagged
[1,2,3,4,5].iter()
.inspect(|&x| println!("inspect the number: {}", x))
.for_each(|&x| {
assert!(x >= 0);
});
Write this instead
[1,2,3,4,5].iter()
.for_each(|&x| {
println!("inspect the number: {}", x);
assert!(x >= 0);
});
Turn it off, or on
Allow it where it does not fit your code, as narrowly as you can:
On one item
#[allow(clippy::inspect_for_each)]
fn my_function() { /* ... */ }Whole crate (Cargo.toml)
[lints.clippy]
inspect_for_each = "allow"One run
cargo clippy -- -A clippy::inspect-for-eachThe [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 usage of inspect().for_each().
Why is this bad?
It is the same as performing the computation
inside inspect at the beginning of the closure in for_each.
Example
[1,2,3,4,5].iter()
.inspect(|&x| println!("inspect the number: {}", x))
.for_each(|&x| {
assert!(x >= 0);
});
Can be written as
[1,2,3,4,5].iter()
.for_each(|&x| {
println!("inspect the number: {}", x);
assert!(x >= 0);
});
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
- get_last_with_lenaccessing last element with `x.get(x.len() - 1)`
- identity_opChecks for identity operations, e.g., x + 0
- implied_bounds_in_implsLooks for bounds in impl Trait in return position that are implied by other bounds. This can happen when a trait is specified that another trait already has as a supertrait (e.g
- int_plus_oneunnecessary `>= y + 1` or `x - 1 >=`
- iter_countcalled `.iter().count()` on a `Vec`
- iter_kv_mapChecks for iterating a map (HashMap or BTreeMap) and ignoring either the keys or values
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?