RS.CLIPPY.BIND_INSTEAD_OF_MAP
Using `Option.and_then(|x| Some(y))`, which is more succinctly expressed as `map(|x| y)`
This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: bind_instead_of_map. Copyright ©2025 The Rust Team. All rights reserved.
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