Skip to content

map_unwrap_or should cover Result::unwrap_or #15714

@TomFryersMidsummer

Description

@TomFryersMidsummer

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-negativeIssue: The lint should have been triggered on code, but wasn't

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions