You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Detects match guards that can be collapsed into the pattern.
Categories (optional)
Kind: complexity
What is the advantage of the recommended code over the original code
It is simpler.
Drawbacks
None.
Example
match x {Some(x)ifmatches!(x,Some(1)) => ..,// if matches!Some(x)if x == Some(2) => ..,// if ==
x => matches!(x,Some(x)if x == 3),// guard inside matches!}
Could be written as:
match x {Some(Some(1)) => ..,Some(Some(2)) => ..,
x => matches!(x,Some(3)),}
This lint can share some logic with equatable_if_let. @HKalbasi maybe you'd like to take this?
The text was updated successfully, but these errors were encountered:
I came here to suggest a similar lint. While I was learning rust I was using guards where I should instead be using ranges eg:
match x {
n if n < 10 => { ...},
n if n >= 10 && n <= 20 => { ...},
n if n > 20 => { ...},
_ => unreachable!(),}
and was frustrated that the compiler was requiring the unreachable clause. After looking at this a bit I discovered I really should have been writing this as:
match x {0..10 @ n => { ...},10..=20 @ n => { ...},20.. @ n => { ...},}
The advantages here are both simplicity, teaching new rustaceans how to use pattern matching better, as well as additional safety guarantees. This also no longer needs the unreachable clause at the end.
What it does
Detects match guards that can be collapsed into the pattern.
Categories (optional)
What is the advantage of the recommended code over the original code
It is simpler.
Drawbacks
None.
Example
Could be written as:
This lint can share some logic with
equatable_if_let
. @HKalbasi maybe you'd like to take this?The text was updated successfully, but these errors were encountered: