]> git.lizzy.rs Git - rust.git/blob - tests/ui/neg_cmp_op_on_partial_ord.rs
Merge pull request #2984 from flip1995/single_char_pattern
[rust.git] / 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 #[warn(neg_cmp_op_on_partial_ord)]
8 fn main() {
9
10     let a_value = 1.0;
11     let another_value = 7.0;
12
13     // --- Bad ---
14
15
16     // Not Less but potentially Greater, Equal or Uncomparable.
17     let _not_less = !(a_value < another_value);
18     
19     // Not Less or Equal but potentially Greater or Uncomparable.
20     let _not_less_or_equal = !(a_value <= another_value);
21
22     // Not Greater but potentially Less, Equal or Uncomparable.
23     let _not_greater = !(a_value > another_value);
24
25     // Not Greater or Equal but potentially Less or Uncomparable.
26     let _not_greater_or_equal = !(a_value >= another_value);
27
28
29     // --- Good ---
30
31
32     let _not_less = match a_value.partial_cmp(&another_value) {
33         None | Some(Ordering::Greater) | Some(Ordering::Equal)  => true,
34         _ => false,
35     };
36     let _not_less_or_equal = match a_value.partial_cmp(&another_value) {
37         None | Some(Ordering::Greater) => true,
38         _ => false,
39     };
40     let _not_greater = match a_value.partial_cmp(&another_value) {
41         None | Some(Ordering::Less) | Some(Ordering::Equal) => true,
42         _ => false,
43     };
44     let _not_greater_or_equal = match a_value.partial_cmp(&another_value) {
45         None | Some(Ordering::Less) => true,
46         _ => false,
47     };
48
49
50     // --- Should not trigger ---
51
52
53     let _ = a_value < another_value;
54     let _ = a_value <= another_value;
55     let _ = a_value > another_value;
56     let _ = a_value >= another_value;
57
58     // --- regression tests ---
59
60     // Issue 2856: False positive on assert!()
61     //
62     // The macro always negates the result of the given comparison in its
63     // internal check which automatically triggered the lint. As it's an
64     // external macro there was no chance to do anything about it which led
65     // to a whitelisting of all external macros.
66     assert!(a_value < another_value);
67 }