-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Description
The comparison_chain
lint is defined as:
Checks comparison chains written with if that can be rewritten with match and cmp.
However it also triggers on comparison chains written with if who's compared type does not implement Ord
and doesn't offer cmp
.
One such example would be f64
. This type implements PartialOrd
but not Ord
. It provides <
and >
operators without providing cmp
.
While in theory the same concept of using match
could also be applied to the result of partial_cmp
, I personally find matching over Optional<Ordering>
much less appealing than matching over Ordering
.
Should we apply the same lint to all PartialOrd
cases and change the documentation to make this the official desired behavior? Or should we consider this a bug and change the implementation, so that the lint only fires if cmp
is available on the relevant type (as specified in the documentation)?
cc @james9909
Example implementation where clippy::comparison_chain
fires, eventough cmp
is not available:
fn f(x: f64, y: f64) {
if x > y {
a()
} else if x < y {
b()
} else {
c()
}
}
Reproduced on:
clippy 0.0.212 (3aea860 2019-09-03)
clippy 0.0.212 (4e7e71b 2019-10-11)
Activity
flip1995 commentedon Nov 19, 2019
We should only lint for types, that implement
Ord
. It would be possible to implement the same lint forPartialOrd
, but put it in thepedantic
group.[-]`clippy::comparison_chain` false positive on PartialOrd or desired behavior[/-][+]`clippy::comparison_chain` false positive on PartialOrd[/+][comparison_chain] rust-lang#4827 Check `core::cmp::Ord` is implemented
core::cmp::Ord
is implemented #4842[comparison_chain] rust-lang#4827 Check `core::cmp::Ord` is implemented
Rollup merge of rust-lang#4842 - timbodeit:comparison-chain-false-pos…
Auto merge of #4855 - phansch:rollup-x7yail7, r=phansch
cmp
match #4531submodules: update clippy from 7b8e829 to 7a943a9