]> git.lizzy.rs Git - rust.git/blob - tests/ui/absurd-extreme-comparisons.rs
rustup and compile-fail -> ui test move
[rust.git] / tests / ui / absurd-extreme-comparisons.rs
1 #![feature(plugin)]
2 #![plugin(clippy)]
3
4 #![deny(absurd_extreme_comparisons)]
5 #![allow(unused, eq_op, no_effect, unnecessary_operation)]
6
7 fn main() {
8     const Z: u32 = 0;
9
10     let u: u32 = 42;
11
12     u <= 0;
13     //~^ ERROR this comparison involving the minimum or maximum element for this type contains a
14     //~| HELP using u == 0 instead
15     u <= Z;
16     //~^ ERROR this comparison involving
17     //~| HELP using u == Z instead
18     u < Z;
19     //~^ ERROR this comparison involving
20     //~| HELP comparison is always false
21     Z >= u;
22     //~^ ERROR this comparison involving
23     //~| HELP using Z == u instead
24     Z > u;
25     //~^ ERROR this comparison involving
26     //~| HELP comparison is always false
27     u > std::u32::MAX;
28     //~^ ERROR this comparison involving
29     //~| HELP comparison is always false
30     u >= std::u32::MAX;
31     //~^ ERROR this comparison involving
32     //~| HELP using u == std::u32::MAX instead
33     std::u32::MAX < u;
34     //~^ ERROR this comparison involving
35     //~| HELP comparison is always false
36     std::u32::MAX <= u;
37     //~^ ERROR this comparison involving
38     //~| HELP using std::u32::MAX == u instead
39
40     1-1 > u;
41         //~^ ERROR this comparison involving
42         //~| HELP because 1-1 is the minimum value for this type, this comparison is always false
43     u >= !0;
44         //~^ ERROR this comparison involving
45         //~| HELP consider using u == !0 instead
46     u <= 12 - 2*6;
47         //~^ ERROR this comparison involving
48         //~| HELP consider using u == 12 - 2*6 instead
49
50     let i: i8 = 0;
51     i < -127 - 1;
52     //~^ ERROR this comparison involving
53     //~| HELP comparison is always false
54     std::i8::MAX >= i;
55     //~^ ERROR this comparison involving
56     //~| HELP comparison is always true
57     3-7 < std::i32::MIN;
58     //~^ ERROR this comparison involving
59     //~| HELP comparison is always false
60
61     let b = false;
62     b >= true;
63     //~^ ERROR this comparison involving
64     //~| HELP using b == true instead
65     false > b;
66     //~^ ERROR this comparison involving
67     //~| HELP comparison is always false
68
69     u > 0; // ok
70
71     // this is handled by unit_cmp
72     () < {}; //~WARNING <-comparison of unit values detected.
73 }
74
75 use std::cmp::{Ordering, PartialEq, PartialOrd};
76
77 #[derive(PartialEq, PartialOrd)]
78 pub struct U(u64);
79
80 impl PartialEq<u32> for U {
81     fn eq(&self, other: &u32) -> bool {
82         self.eq(&U(*other as u64))
83     }
84 }
85 impl PartialOrd<u32> for U {
86     fn partial_cmp(&self, other: &u32) -> Option<Ordering> {
87         self.partial_cmp(&U(*other as u64))
88     }
89 }
90
91 pub fn foo(val: U) -> bool {
92     val > std::u32::MAX
93 }