-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Open
Labels
C-bugCategory: Clippy is not doing the correct thingCategory: Clippy is not doing the correct thingI-false-negativeIssue: The lint should have been triggered on code, but wasn'tIssue: The lint should have been triggered on code, but wasn't
Description
Summary
It currently covers
/ | Option |
Result |
---|---|---|
unwrap_or |
Option::unwrap_or |
mysterious gap |
unwrap_or_else |
Option::unwrap_or_else |
Result::unwrap_or_else . |
Lint Name
map_unwrap_or
Reproducer
I tried this code:
#[deny(clippy::map_unwrap_or)]
fn main() {
let o: Option<i32> = Some(3);
let r: Result<i32, ()> = Ok(3);
println!("{}", o.map(|y| y + 1).unwrap_or(3));
println!("{}", o.map(|y| y + 1).unwrap_or_else(|| 3));
println!("{}", r.map(|y| y + 1).unwrap_or(3));
println!("{}", r.map(|y| y + 1).unwrap_or_else(|()| 3));
}
I expected to see this happen: four similar warnings.
Instead, this happened:
$ clippy-driver main.rs
error: called `map(<f>).unwrap_or(<a>)` on an `Option` value
--> main.rs:5:20
|
5 | println!("{}", o.map(|y| y + 1).unwrap_or(3));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#map_unwrap_or
note: the lint level is defined here
--> main.rs:1:8
|
1 | #[deny(clippy::map_unwrap_or)]
| ^^^^^^^^^^^^^^^^^^^^^
help: use `map_or(<a>, <f>)` instead
|
5 - println!("{}", o.map(|y| y + 1).unwrap_or(3));
5 + println!("{}", o.map_or(3, |y| y + 1));
|
error: called `map(<f>).unwrap_or_else(<g>)` on an `Option` value
--> main.rs:6:20
|
6 | println!("{}", o.map(|y| y + 1).unwrap_or_else(|| 3));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `o.map_or_else(|| 3, |y| y + 1)`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#map_unwrap_or
error: called `map(<f>).unwrap_or_else(<g>)` on a `Result` value
--> main.rs:8:20
|
8 | println!("{}", r.map(|y| y + 1).unwrap_or_else(|()| 3));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `r.map_or_else(|()| 3, |y| y + 1)`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#map_unwrap_or
error: aborting due to 3 previous errors
Version
rustc 1.90.0 (1159e78c4 2025-09-14)
binary: rustc
commit-hash: 1159e78c4747b02ef996e55082b704c09b970588
commit-date: 2025-09-14
host: x86_64-unknown-linux-gnu
release: 1.90.0
LLVM version: 20.1.8
zihan0822
Metadata
Metadata
Assignees
Labels
C-bugCategory: Clippy is not doing the correct thingCategory: Clippy is not doing the correct thingI-false-negativeIssue: The lint should have been triggered on code, but wasn'tIssue: The lint should have been triggered on code, but wasn't