-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
==
check instead of opt.map_or(false, |val| val == n)
or if let Some(n) = opt
#10118
Comments
Note that depending on the
This is a valid type with a valid What we might try is check if the |
Good point but yes we can do that, or we can check if it's equal to the pattern match (not sure if you can run code while linting) Another consideration though is if they implemented it custom, maybe they don't want you to use pattern matching, for example: enum Num {
Small(u8),
Large(u64),
}
assert_eq!(Num::Small(5), Num::Large(5)); |
We can run code while linting, but only constant code, which won't be too helpful in this case. Apart from checking if the impl is derived, we could also just have a list of types to check for that might be user-extensible via config. |
They could just allow the lint in that case, I think checking if the impl is derived is our best bet then |
There is still a difference between allowing the lint wholesale or giving the user the option to disable it by type. |
I meant allowing it for a module/method but the config makes sense too |
I can take a crack at implementing this. @rustbot assign |
Whoops @rustbot claim |
Closes #10118 This lint checks `map_or` method calls to check if they can be consolidated down to something simpler and/or more readable. For example, the code ```rs let x = Some(5); x.map_or(false, |n| n == 5) ``` can be rewritten as ```rs let x = Some(5); x == Some(5) ``` In addition, when the closure is more complex, the code can be altered from, say, ```rs let x = Ok::<Vec<i32>, i32>(vec![5]); x.map_or(false, |n| n == [5]) ``` into ```rs let x = Ok::<Vec<i32>, i32>(vec![5]); x.is_some_and(|n| n == [5]) ``` This lint also considers cases where the `map_or` can be chained with other method calls, and accommodates accordingly by adding extra parentheses as needed to the suggestion. changelog: add new lint `unnecessary_map_or`
What it does
It converts a code like
to
This could apply to
Result
and technically any enum wrapping a value, for exampleto
Lint Name
Unnecessary pattern matching
Category
style
Advantage
Drawbacks
The enum needs to implement
PartialEq
Example
Could be written as:
The text was updated successfully, but these errors were encountered: