-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
should comparison_chain only trigger for "if x > y {} else if x <y {} else {}" ? #4725
Comments
Or suggest if let Ordering::Greater = _ {
} else if let Ordering::Less = _ {
} |
I don't understand this suggestion. Condition is checking if random_size > 0.
|
The lint suggest that you rewrite it with match random_size.cmp(&0) {
Ordering::Greater => { .. },
_ => {},
} which admittedly makes the code worse, IMO. We shouldn't lint, if literals are involved. |
@rohitjoshi Are you saying that it can lint when there is just an For the original suggestion, I agree, but only for primitive types. For large, complex types, it is good to avoid the second comparison, maybe for strings too. |
Yes, with if statement comparing with const value 0, It shows suggestion to replace with match/compare statement. |
Can you reproduce that in a Rust playground? I wasn't able to. |
Sorry, it does have else if < 0 condition. |
This code
Triggers
clippy::comparison_chain
:however using a match forces us to cover one additional case which the if-construct does not have:
Ordering::Equal
is not covered originally and we would have to use something like_ => {}
to get our code to compile.Perhaps the lint should be refactored to only warn if there is also a final
else
/if there are 3 cases that are handled by the if-condition construct?clippy 0.0.212 (e8d5a9e 2019-10-22)
The text was updated successfully, but these errors were encountered: