rust-lang / rust-lang/rust-clippy

Recommend `match` instead of `.map().unwrap_or_else()`

Open
#17,470 6 comments 1 reaction 0 assignees View on GitHub

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 match is 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.