rust-lang / rust-lang/rust-clippy

Disallow `.map_err(...).ok()` pattern

Open
#8,795 1 comment 0 reactions 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

I recently observed a code pattern where a Result was converted to an Option with an unnecessary map_err invocation along the way.

Lint Name

map_err_ok

Category

restriction

Advantage

Removes a call to map_err where the error value that is generated is immediately discarded.

Drawbacks

No response

Example
let x = something_that_returns_result()
    .map_err(|_e| Error::SomeOtherError)
    .ok();

Could be written as:

let x = something_that_returns_result().ok();

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

The requested entry point is the map_err_ok restriction lint; start by reviewing comparable restriction lints in rust-clippy and the example pattern in the issue. Add coverage for the shown Result-to-Option case and verify that the lint flags the redundant map_err while preserving the direct .ok() form.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.