rust-lang / rust-lang/rust-clippy

Suggest replacing `result.map_or_else(Err, f)` with `result.and_then(f)`

Open
#17,322 4 comments 1 reaction 0 assignees View on GitHub

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

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.