Closed
Description
What it does
It converts a code like
opt.map_or(false, |val| val == 5)
to
opt == Some(5);
This could apply to Result
and technically any enum wrapping a value, for example
matches!(err, Error::Io(5));
to
err == Error::Io(5);
Lint Name
Unnecessary pattern matching
Category
style
Advantage
- Readability
- Conciseness
- Possible optimization (Could someone that can read assembly verify this? The resulting assembly has fewer lines)
Drawbacks
The enum needs to implement PartialEq
Example
#[derive(PartialEq)]
enum Error {
Io(u16),
Os(u16),
}
fn unnecessary_pattern_match() {
let err = Error::Os(1);
matches!(err, Error::Io(5));
}
fn unnecessary_map_or() {
let opt = Some(1);
opt.map_or(false, |val| val == 5);
}
Could be written as:
#[derive(PartialEq)]
enum Error {
Io(u16),
Os(u16),
}
fn unnecessary_pattern_match() {
let err = Error::Os(1);
err == Error::Io(5);
}
fn unnecessary_map_or() {
let opt = Some(1);
opt == Some(5);
}