Clippy lints / Complexity
Clippy lint · Complexity · checked with Clippy 0.1.99
clippy::bind_instead_of_map: Checks for usage of _.and_then(|x| Some(y)), _.and_then(|x| Ok(y)) or _.or_else(|x| Err(y))
What it meansChecks for usage of _.and_then(|x| Some(y)), _.and_then(|x| Ok(y)) or _.or_else(|x| Err(y)). It is in the complexity group and warns by default.
Does it matter?
This can be written more concisely as _.map(|x| y) or _.map_err(|x| y).
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 _ = opt().and_then(|s| Some(s.len()));
let _ = res().and_then(|s| if s.len() == 42 { Ok(10) } else { Ok(20) });
let _ = res().or_else(|s| if s.len() == 42 { Err(10) } else { Err(20) });
Write this instead
let _ = opt().map(|s| s.len());
let _ = res().map(|s| if s.len() == 42 { 10 } else { 20 });
let _ = res().map_err(|s| if s.len() == 42 { 10 } else { 20 });
Turn it off, or on
Allow it where it does not fit your code, as narrowly as you can:
On one item
#[allow(clippy::bind_instead_of_map)]
fn my_function() { /* ... */ }Whole crate (Cargo.toml)
[lints.clippy]
bind_instead_of_map = "allow"One run
cargo clippy -- -A clippy::bind-instead-of-mapThe [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 _.and_then(|x| Some(y)), _.and_then(|x| Ok(y))
or _.or_else(|x| Err(y)).
Why is this bad?
This can be written more concisely as _.map(|x| y) or _.map_err(|x| y).
Example
let _ = opt().and_then(|s| Some(s.len()));
let _ = res().and_then(|s| if s.len() == 42 { Ok(10) } else { Ok(20) });
let _ = res().or_else(|s| if s.len() == 42 { Err(10) } else { Err(20) });
The correct use would be:
let _ = opt().map(|s| s.len());
let _ = res().map(|s| if s.len() == 42 { 10 } else { 20 });
let _ = res().map_err(|s| if s.len() == 42 { 10 } else { 20 });
Past names
- option_and_then_some
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
- bool_comparisonChecks for expressions of the form x == true, x != true and order comparisons such as x < true (or vice versa) and suggest using the variable directly
- borrow_deref_refderef on an immutable reference
- borrowed_boxChecks for usage of &Box<T> anywhere in the code. Check the Box documentation for more information
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?