Clippy lints / Complexity
Clippy lint · Complexity · checked with Clippy 0.1.99
clippy::map_flatten: called `map(..).flatten()` on `Iterator`
What it meansChecks for usage of _.map(_).flatten(_) on Iterator, Option, and Result. It is in the complexity group and warns by default.
What you see
warning: called `map(..).flatten()` on `Iterator` --> main.rs:4:12 | 4 | vec.iter().map(|x| x.iter()).flatten(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try replacing `map` with `flat_map` and remove the `.flatten()`: `flat_map(|x| x.iter())` | = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#map_flatten = note: `#[warn(clippy::map_flatten)]` 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?
Readability. This can be written more concisely with Iterator::flat_map, Option::and_then, and Result::and_then.
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 vec = vec![vec![1]];
vec.iter().map(|x| x.iter()).flatten();
let opt = Some(5);
opt.map(|x| Some(x * 2)).flatten();
let res: Result<i32, String> = Ok(5);
res.map(|x| Ok(x * 2)).flatten();
Write this instead
let vec = vec![vec![1]];
vec.iter().flat_map(|x| x.iter());
let opt = Some(5);
opt.and_then(|x| Some(x * 2));
let res: Result<i32, String> = Ok(5);
res.and_then(|x| Ok(x * 2));
Turn it off, or on
Allow it where it does not fit your code, as narrowly as you can:
On one item
#[allow(clippy::map_flatten)]
fn my_function() { /* ... */ }Whole crate (Cargo.toml)
[lints.clippy]
map_flatten = "allow"One run
cargo clippy -- -A clippy::map-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.
Clippy's own documentation
What it does
Checks for usage of _.map(_).flatten(_) on Iterator, Option, and Result.
Why is this bad?
Readability. This can be written more concisely with Iterator::flat_map, Option::and_then, and
Result::and_then.
Example
let vec = vec![vec![1]];
vec.iter().map(|x| x.iter()).flatten();
let opt = Some(5);
opt.map(|x| Some(x * 2)).flatten();
let res: Result<i32, String> = Ok(5);
res.map(|x| Ok(x * 2)).flatten();
Use instead:
let vec = vec![vec![1]];
vec.iter().flat_map(|x| x.iter());
let opt = Some(5);
opt.and_then(|x| Some(x * 2));
let res: Result<i32, String> = Ok(5);
res.and_then(|x| Ok(x * 2));
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_takemanual implementation of `mem::take`
- manual_unwrap_orthis pattern reimplements `Option::unwrap_or`
- map_all_any_identityChecks for usage of .map(…), followed by .all(identity) or .any(identity)
- map_identityunnecessary map of the identity function
- map_or_identityexpression can be simplified using `Option::unwrap_or()`
- match_as_refmanual implementation of `Option::as_ref`
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?