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
@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::mapandResult::map_erris 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
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.