2018-06-20 04:07:41 -05:00
|
|
|
//! This test case utilizes `f64` an easy example for `PartialOrd` only types
|
|
|
|
//! but the lint itself actually validates any expression where the left
|
|
|
|
//! operand implements `PartialOrd` but not `Ord`.
|
2018-06-01 04:58:40 -05:00
|
|
|
|
|
|
|
use std::cmp::Ordering;
|
|
|
|
|
2020-07-14 07:59:59 -05:00
|
|
|
#[allow(clippy::unnested_or_patterns, clippy::match_like_matches_macro)]
|
2018-07-28 10:34:52 -05:00
|
|
|
#[warn(clippy::neg_cmp_op_on_partial_ord)]
|
2018-06-01 04:58:40 -05:00
|
|
|
fn main() {
|
|
|
|
let a_value = 1.0;
|
|
|
|
let another_value = 7.0;
|
|
|
|
|
|
|
|
// --- Bad ---
|
|
|
|
|
|
|
|
// Not Less but potentially Greater, Equal or Uncomparable.
|
|
|
|
let _not_less = !(a_value < another_value);
|
2018-10-11 05:16:22 -05:00
|
|
|
|
2018-06-01 04:58:40 -05:00
|
|
|
// Not Less or Equal but potentially Greater or Uncomparable.
|
|
|
|
let _not_less_or_equal = !(a_value <= another_value);
|
|
|
|
|
|
|
|
// Not Greater but potentially Less, Equal or Uncomparable.
|
|
|
|
let _not_greater = !(a_value > another_value);
|
|
|
|
|
|
|
|
// Not Greater or Equal but potentially Less or Uncomparable.
|
|
|
|
let _not_greater_or_equal = !(a_value >= another_value);
|
|
|
|
|
|
|
|
// --- Good ---
|
|
|
|
|
|
|
|
let _not_less = match a_value.partial_cmp(&another_value) {
|
2018-12-09 16:26:16 -06:00
|
|
|
None | Some(Ordering::Greater) | Some(Ordering::Equal) => true,
|
2018-06-01 07:40:53 -05:00
|
|
|
_ => false,
|
2018-06-01 04:58:40 -05:00
|
|
|
};
|
|
|
|
let _not_less_or_equal = match a_value.partial_cmp(&another_value) {
|
|
|
|
None | Some(Ordering::Greater) => true,
|
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
let _not_greater = match a_value.partial_cmp(&another_value) {
|
|
|
|
None | Some(Ordering::Less) | Some(Ordering::Equal) => true,
|
2018-06-01 07:40:53 -05:00
|
|
|
_ => false,
|
2018-06-01 04:58:40 -05:00
|
|
|
};
|
|
|
|
let _not_greater_or_equal = match a_value.partial_cmp(&another_value) {
|
|
|
|
None | Some(Ordering::Less) => true,
|
2018-06-01 07:40:53 -05:00
|
|
|
_ => false,
|
2018-06-01 04:58:40 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// --- Should not trigger ---
|
|
|
|
|
|
|
|
let _ = a_value < another_value;
|
|
|
|
let _ = a_value <= another_value;
|
|
|
|
let _ = a_value > another_value;
|
|
|
|
let _ = a_value >= another_value;
|
|
|
|
|
2018-06-20 04:07:41 -05:00
|
|
|
// --- regression tests ---
|
|
|
|
|
|
|
|
// Issue 2856: False positive on assert!()
|
|
|
|
//
|
2018-06-29 09:55:26 -05:00
|
|
|
// The macro always negates the result of the given comparison in its
|
2018-06-20 04:07:41 -05:00
|
|
|
// internal check which automatically triggered the lint. As it's an
|
2018-06-29 09:55:26 -05:00
|
|
|
// external macro there was no chance to do anything about it which led
|
2020-07-07 10:12:44 -05:00
|
|
|
// to an exempting of all external macros.
|
2018-06-20 04:07:41 -05:00
|
|
|
assert!(a_value < another_value);
|
|
|
|
}
|