]> git.lizzy.rs Git - rust.git/blob - tests/ui/neg_cmp_op_on_partial_ord.rs
Auto merge of #3603 - xfix:random-state-lint, r=phansch
[rust.git] / tests / ui / neg_cmp_op_on_partial_ord.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 //! This test case utilizes `f64` an easy example for `PartialOrd` only types
11 //! but the lint itself actually validates any expression where the left
12 //! operand implements `PartialOrd` but not `Ord`.
13
14 use std::cmp::Ordering;
15
16 #[warn(clippy::neg_cmp_op_on_partial_ord)]
17 fn main() {
18     let a_value = 1.0;
19     let another_value = 7.0;
20
21     // --- Bad ---
22
23     // Not Less but potentially Greater, Equal or Uncomparable.
24     let _not_less = !(a_value < another_value);
25
26     // Not Less or Equal but potentially Greater or Uncomparable.
27     let _not_less_or_equal = !(a_value <= another_value);
28
29     // Not Greater but potentially Less, Equal or Uncomparable.
30     let _not_greater = !(a_value > another_value);
31
32     // Not Greater or Equal but potentially Less or Uncomparable.
33     let _not_greater_or_equal = !(a_value >= another_value);
34
35     // --- Good ---
36
37     let _not_less = match a_value.partial_cmp(&another_value) {
38         None | Some(Ordering::Greater) | Some(Ordering::Equal) => true,
39         _ => false,
40     };
41     let _not_less_or_equal = match a_value.partial_cmp(&another_value) {
42         None | Some(Ordering::Greater) => true,
43         _ => false,
44     };
45     let _not_greater = match a_value.partial_cmp(&another_value) {
46         None | Some(Ordering::Less) | Some(Ordering::Equal) => true,
47         _ => false,
48     };
49     let _not_greater_or_equal = match a_value.partial_cmp(&another_value) {
50         None | Some(Ordering::Less) => true,
51         _ => false,
52     };
53
54     // --- Should not trigger ---
55
56     let _ = a_value < another_value;
57     let _ = a_value <= another_value;
58     let _ = a_value > another_value;
59     let _ = a_value >= another_value;
60
61     // --- regression tests ---
62
63     // Issue 2856: False positive on assert!()
64     //
65     // The macro always negates the result of the given comparison in its
66     // internal check which automatically triggered the lint. As it's an
67     // external macro there was no chance to do anything about it which led
68     // to a whitelisting of all external macros.
69     assert!(a_value < another_value);
70 }