rust-lang / rust-lang/rust-clippy
Lint map_or branches that return the same constant
@ivanlomeli is already working on this.
Since Aug 17, 2026.
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
What it does
Warn when Option::map_or, Result::map_or, or their map_or_else variants use the same constant result for both branches. For example, result.map_or(false, |_| false) is always false, regardless of whether the result is Ok or Err.
This is likely a copy/paste or logic error, so the lint should be in the suspicious category rather than a style simplification.
Advantage
- Catches boolean branch typos that otherwise compile and silently produce a constant result.
- Makes it clear when the mapped value or variant is accidentally ignored.
Drawbacks
The constant result can be intentional. A machine-applicable replacement is also not always possible because evaluating the receiver can have side effects or observable drop order. The lint may therefore need to warn without an automatic suggestion unless preserving evaluation semantics is proven safe.
Example
fn is_valid(result: Result<u32, ()>) -> bool {
result.map_or(false, |_| false)
}
If the second false was a typo, this could be written as:
fn is_valid(result: Result<u32, ()>) -> bool {
result.is_ok()
}
The same issue applies to lazy defaults:
let value = option.map_or_else(|| true, |_| true);
Comparison with existing lints
clippy::unnecessary_map_or can simplify opposite boolean branches to variant queries such as is_ok() or is_err(). Equal constant branches are different: they make the result independent of the variant and are more likely to indicate a logic bug than a style issue.
Additional context
This case came up while reviewing the Result boolean handling in rust-lang/rust-clippy#17537. It was intentionally kept separate from that style-lint change.
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.
Assessment
This issue has not been assessed yet.