Clippy lints / Suspicious
Clippy lint · Suspicious · checked with Clippy 0.1.99
clippy::unnecessary_option_map_or_else: unused "map closure" when calling `Option::map_or_else` value
What it meansChecks for usage of .map_or_else() “map closure” for Option type. It is in the suspicious group and warns by default.
What you see
warning: unused "map closure" when calling `Option::map_or_else` value --> main.rs:5:9 | 5 | let y = x.map_or_else(|| 2 * k, |n| n); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#unnecessary_option_map_or_else = note: `#[warn(clippy::unnecessary_option_map_or_else)]` on by default help: consider using `unwrap_or_else` | 5 - let y = x.map_or_else(|| 2 * k, |n| n); 5 + let y = x.unwrap_or_else(|| 2 * k); |
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?
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 k = 10;
let x: Option<u32> = Some(4);
let y = x.map_or_else(|| 2 * k, |n| n);
Write this instead
let k = 10;
let x: Option<u32> = Some(4);
let y = x.unwrap_or_else(|| 2 * k);
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_option_map_or_else)]
fn my_function() { /* ... */ }Whole crate (Cargo.toml)
[lints.clippy]
unnecessary_option_map_or_else = "allow"One run
cargo clippy -- -A clippy::unnecessary-option-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 Option type.
Why is this bad?
This can be written more concisely by using unwrap_or_else().
Example
let k = 10;
let x: Option<u32> = Some(4);
let y = x.map_or_else(|| 2 * k, |n| n);
Use instead:
let k = 10;
let x: Option<u32> = Some(4);
let y = x.unwrap_or_else(|| 2 * k);
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- type_id_on_boxcalling `.type_id()` on `Box<dyn Any>`
- unconditional_recursionfunction cannot return without recursing
- unnecessary_clippy_cfgno need to put clippy lints behind a `clippy` cfg
- unnecessary_result_map_or_elseChecks for usage of .map_or_else() “map closure” for Result type
- 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::type_id_on_boxcalling `.type_id()` on `Box<dyn Any>`Read it next →Did this answer your question?