Clippy lints / Suspicious
Clippy lint · Suspicious · checked with Clippy 0.1.99
clippy::unnecessary_result_map_or_else: Checks for usage of .map_or_else() “map closure” for Result type
What it meansChecks for usage of .map_or_else() “map closure” for Result type. It is in the suspicious group and warns by default.
Does it matter?
This can be written more concisely by using unwrap_or_else().
Code that is most likely wrong or useless. 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: Result<u32, ()> = Ok(0);
let y = x.map_or_else(|err| handle_error(err), |n| n);
Write this instead
let x: Result<u32, ()> = Ok(0);
let y = x.unwrap_or_else(|err| handle_error(err));
Turn it off, or on
Allow it where it does not fit your code, as narrowly as you can:
On one item
#[allow(clippy::unnecessary_result_map_or_else)]
fn my_function() { /* ... */ }Whole crate (Cargo.toml)
[lints.clippy]
unnecessary_result_map_or_else = "allow"One run
cargo clippy -- -A clippy::unnecessary-result-map-or-elseThe [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_or_else() “map closure” for Result type.
Why is this bad?
This can be written more concisely by using unwrap_or_else().
Example
let x: Result<u32, ()> = Ok(0);
let y = x.map_or_else(|err| handle_error(err), |n| n);
Use instead:
let x: Result<u32, ()> = Ok(0);
let y = x.unwrap_or_else(|err| handle_error(err));
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- unconditional_recursionfunction cannot return without recursing
- unnecessary_clippy_cfgno need to put clippy lints behind a `clippy` cfg
- unnecessary_option_map_or_elseunused "map closure" when calling `Option::map_or_else` value
- zero_repeat_side_effectsexpression with side effects as the initial value in a zero-sized array initializer
- zombie_processesspawned process is never `wait()`ed on
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::unconditional_recursionfunction cannot return without recursingRead it next →Did this answer your question?