rust-lang / rust-lang/rust-clippy
Suggest replacing `result.map_or_else(Err, f)` with `result.and_then(f)`
Open
Nobody has claimed this yet.
C-feature-request
- Dominant language
- Rust
- Stars
- 13.5k
- Forks
- 2.2k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 32
Description
Description
This could be a new case in unnecessary_result_map_or_else, or a new lint.
Advantages
- more concise
- easier to understand (it legit took me a couple minutes to decipher the thing)
Example
fn foo(res: Result<Vec<i32>, String>) -> Result<i32, String> {
res.map_or_else(Err, |mut v| v.pop().ok_or_else(|| "empty list!".to_string()))
}
Could be written as:
fn foo(res: Result<Vec<i32>, String>) -> Result<i32, String> {
res.and_then(|mut v| v.pop().ok_or_else(|| "empty list!".to_string()))
}
Version
rustc 1.96.0 (ac68faa20 2026-05-25)
binary: rustc
commit-hash: ac68faa20c58cbccd01ee7208bf3b6e93a7d7f96
commit-date: 2026-05-25
host: x86_64-unknown-linux-gnu
release: 1.96.0
LLVM version: 22.1.2
Additional Labels
@rustbot label C-feature-request
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 inspecting the existing unnecessary_result_map_or_else lint and compare its behavior with the result.map_or_else(Err, f) example in this issue. Determine whether this belongs as a new case or a separate lint, then add coverage showing the equivalent and_then suggestion and verify the relevant lint tests pass.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 52/100