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