Clippy lints / Complexity
Clippy lint · Complexity · checked with Clippy 0.1.99
clippy::manual_flatten: unnecessary `if let` since only the `Some` variant of the iterator element is used
What it meansChecks for unnecessary if let usage in a for loop where only the Some or Ok variant of the iterator element is used. It is in the complexity group and warns by default.
What you see
warning: unnecessary `if let` since only the `Some` variant of the iterator element is used --> main.rs:4:1 | 4 | / for n in x { 5 | | if let Some(n) = n { 6 | | println!("{}", n); 7 | | } 8 | | } | |_^ | help: try `.flatten()` and remove the `if let` statement in the for loop --> main.rs:5:5 | 5 | / if let Some(n) = n { 6 | | println!("{}", n); 7 | | } | |_____^ = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#manual_flatten = note: `#[warn(clippy::manual_flatten)]` on by default help: try | 4 ~ for n in x.into_iter().flatten() { 5 + println!("{}", n); 6 + } |
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 verbose and can be simplified by first calling the flatten method on the Iterator.
Code that does something simple in a complicated way. Warns by default.
How to fix it
Clippy can fix this one for you: run cargo clippy --fix (add --allow-dirty if you have uncommitted changes). Review the diff before you commit.
Flagged
let x = vec![Some(1), Some(2), Some(3)];
for n in x {
if let Some(n) = n {
println!("{}", n);
}
}
Write this instead
let x = vec![Some(1), Some(2), Some(3)];
for n in x.into_iter().flatten() {
println!("{}", n);
}
Turn it off, or on
Allow it where it does not fit your code, as narrowly as you can:
On one item
#[allow(clippy::manual_flatten)]
fn my_function() { /* ... */ }Whole crate (Cargo.toml)
[lints.clippy]
manual_flatten = "allow"One run
cargo clippy -- -A clippy::manual-flattenThe [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 for unnecessary if let usage in a for loop
where only the Some or Ok variant of the iterator element is used.
Why is this bad?
It is verbose and can be simplified
by first calling the flatten method on the Iterator.
Example
let x = vec![Some(1), Some(2), Some(3)];
for n in x {
if let Some(n) = n {
println!("{}", n);
}
}
Use instead:
let x = vec![Some(1), Some(2), Some(3)];
for n in x.into_iter().flatten() {
println!("{}", n);
}
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
- 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
- manual_filter_map`filter(..).map(..)` can be simplified as `filter_map(..)`
- manual_findmanual implementation of `Iterator::find`
- manual_find_map`find(..).map(..)` can be simplified as `find_map(..)`
- manual_hash_onemanual implementation of `BuildHasher::hash_one`
- manual_inspectusing `map` over `inspect`
- manual_is_multiple_ofChecks for manual implementation of .is_multiple_of() on unsigned integer types
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?