rust-lang / rust-lang/rust-clippy
`unnecessary-option-map-or-else` falsely reported when map closure uses automatic deref
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Summary
When the map closure in an Option::map_or_else call uses automatic dereferencing to match the type of the "or else" value, Clippy sees the map is the identity function and suggests removing it, but removing it gets rid of the automatic dereferencing and causes a type error.
Lint Name
unnecessary-option-map-or-else
Reproducer
I tried this code:
fn main() {
let owned = String::default();
let optional_ref = Some(&owned);
let str = optional_ref.map_or_else(|| "", |s| s);
println!("{str}")
}
I saw this happen:
warning: unused "map closure" when calling `Option::map_or_else` value
--> src/main.rs:4:15
|
4 | let str = optional_ref.map_or_else(|| "", |s| s);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `unwrap_or_else`: `optional_ref.unwrap_or_else(|| "")`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.92.0/index.html#unnecessary_option_map_or_else
= note: `#[warn(clippy::unnecessary_option_map_or_else)]` on by default
Running cargo clippy --fix results in a compile error.
This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust-clippy/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the--broken-codeflag
I expected to see this happen:
No warning should be reported, or a different suggestion should be given which doesn't result in a compilation error.
Version
rustc 1.92.0 (ded5c06cf 2025-12-08)
binary: rustc
commit-hash: ded5c06cf21d2b93bffd5d884aa6e96934ee4234
commit-date: 2025-12-08
host: aarch64-apple-darwin
release: 1.92.0
LLVM version: 21.1.3
Additional Labels
@rustbot label +I-suggestion-causes-error
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the unnecessary-option-map-or-else warning with the Rust playground example, then locate that lint's implementation and its suggestion logic. Verify the behavior with cargo clippy and cargo clippy --fix; done means the automatic-deref case produces no invalid suggestion and the example still compiles.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 48/100