rust-lang / rust-lang/rust-clippy
Recommend `match` instead of `.map().unwrap_or_else()`
Open
Nobody has claimed this yet.
A-lint
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
What it does
Warn against x.map(...).unwrap_or_else(...) on an Option/Result, and recommend a match instead. Similar to https://github.com/rust-lang/rust-clippy/issues/17469.
Advantage
- More readable.
- Clearer to the reader and to the compiler that the
matchis exhaustive.
Drawbacks
None that I can think of, other than style and personal taste.
Example
opt.map(f).unwrap_or_else(g)
Could be written as:
match opt {
Some(x) => f(x),
None => g(),
}
And for a Result:
match res {
Ok(x) => f(x),
Err(e) => g(e),
}
Comparison with existing lints
No response
Additional Context
No response
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 reviewing the related Clippy issue #17469 and the existing lint patterns for Rust Option and Result expressions. Implement the warning for the shown map(...).unwrap_or_else(...) forms, cover both Option and Result examples, and verify the resulting diagnostics and tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 65/100