suggest `map` or `map_err` when only the `Ok()` or `Err()` values are changed, respectively, in a pattern match
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.
@rustbot claim
As a past master of the unnecessary match on Result (I've since gotten better), I definitely support the existence of this lint -- I'd like clippy to check my old code for cases where past-me was being a goose. @darklyspaced, are you still interested in implementing this? Otherwise, I could have a crack at it.
yes, feel to have a go at it. i don't have much time nowadays unfortunately.
@rustbot claim