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