rust-lang / rust-lang/rust-clippy

suggest `map` or `map_err` when only the `Ok()` or `Err()` values are changed, respectively, in a pattern match

Open
#11,211 5 comments 0 reactions 1 assignee View on GitHub

@mpalmer is already working on this.

Since May 14, 2024.

A-lint
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

What it does

Checks for usage of a match of if let expression where only the Ok() or Err() values are modified; suggests the use of map or map_err instead.

Advantage
  • Readability, usage of Result::map and Result::map_err is more concise
Drawbacks

None that I can think of.

Example
#[derive(PartialEq, Debug)]
struct Wrapper(i32);

let x = match fallible() {
    Ok(a) => Ok(Wrapper(a)),
    Err(err) => Err(err),
};

Could be written as:

# #[derive(PartialEq, Debug)]
# struct Wrapper(i32);

let x = fallible().map(|ok| Wrapper);
Extensions

This would also apply to other pattern matches. For example, if let Ok(x) and modifying just the x value. We could also extend this to map_or_else in the case that both the Ok() and Err() variants are modified.

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.