]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/neg_cmp_op_on_partial_ord.rs
Merge commit '6f50986667debbfc67776304a8ee23fe0158613f' into libgccjit-codegen
[rust.git] / src / tools / clippy / tests / ui / neg_cmp_op_on_partial_ord.rs
1 //! This test case utilizes `f64` an easy example for `PartialOrd` only types
2 //! but the lint itself actually validates any expression where the left
3 //! operand implements `PartialOrd` but not `Ord`.
4
5 use std::cmp::Ordering;
6
7 #[allow(clippy::unnested_or_patterns, clippy::match_like_matches_macro)]
8 #[warn(clippy::neg_cmp_op_on_partial_ord)]
9 fn main() {
10     let a_value = 1.0;
11     let another_value = 7.0;
12
13     // --- Bad ---
14
15     // Not Less but potentially Greater, Equal or Uncomparable.
16     let _not_less = !(a_value < another_value);
17
18     // Not Less or Equal but potentially Greater or Uncomparable.
19     let _not_less_or_equal = !(a_value <= another_value);
20
21     // Not Greater but potentially Less, Equal or Uncomparable.
22     let _not_greater = !(a_value > another_value);
23
24     // Not Greater or Equal but potentially Less or Uncomparable.
25     let _not_greater_or_equal = !(a_value >= another_value);
26
27     // --- Good ---
28
29     let _not_less = match a_value.partial_cmp(&another_value) {
30         None | Some(Ordering::Greater) | Some(Ordering::Equal) => true,
31         _ => false,
32     };
33     let _not_less_or_equal = match a_value.partial_cmp(&another_value) {
34         None | Some(Ordering::Greater) => true,
35         _ => false,
36     };
37     let _not_greater = match a_value.partial_cmp(&another_value) {
38         None | Some(Ordering::Less) | Some(Ordering::Equal) => true,
39         _ => false,
40     };
41     let _not_greater_or_equal = match a_value.partial_cmp(&another_value) {
42         None | Some(Ordering::Less) => true,
43         _ => false,
44     };
45
46     // --- Should not trigger ---
47
48     let _ = a_value < another_value;
49     let _ = a_value <= another_value;
50     let _ = a_value > another_value;
51     let _ = a_value >= another_value;
52
53     // --- regression tests ---
54
55     // Issue 2856: False positive on assert!()
56     //
57     // The macro always negates the result of the given comparison in its
58     // internal check which automatically triggered the lint. As it's an
59     // external macro there was no chance to do anything about it which led
60     // to an exempting of all external macros.
61     assert!(a_value < another_value);
62 }