]> git.lizzy.rs Git - rust.git/blob - tests/ui/absurd-extreme-comparisons.rs
Stabilize tool lints
[rust.git] / tests / ui / absurd-extreme-comparisons.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
12
13
14 #![warn(clippy::absurd_extreme_comparisons)]
15 #![allow(unused, clippy::eq_op, clippy::no_effect, clippy::unnecessary_operation, clippy::needless_pass_by_value)]
16
17 fn main() {
18     const Z: u32 = 0;
19     let u: u32 = 42;
20     u <= 0;
21     u <= Z;
22     u < Z;
23     Z >= u;
24     Z > u;
25     u > std::u32::MAX;
26     u >= std::u32::MAX;
27     std::u32::MAX < u;
28     std::u32::MAX <= u;
29     1-1 > u;
30     u >= !0;
31     u <= 12 - 2*6;
32     let i: i8 = 0;
33     i < -127 - 1;
34     std::i8::MAX >= i;
35     3-7 < std::i32::MIN;
36     let b = false;
37     b >= true;
38     false > b;
39     u > 0; // ok
40     // this is handled by clippy::unit_cmp
41     () < {};
42 }
43
44 use std::cmp::{Ordering, PartialEq, PartialOrd};
45
46 #[derive(PartialEq, PartialOrd)]
47 pub struct U(u64);
48
49 impl PartialEq<u32> for U {
50     fn eq(&self, other: &u32) -> bool {
51         self.eq(&U(u64::from(*other)))
52     }
53 }
54 impl PartialOrd<u32> for U {
55     fn partial_cmp(&self, other: &u32) -> Option<Ordering> {
56         self.partial_cmp(&U(u64::from(*other)))
57     }
58 }
59
60 pub fn foo(val: U) -> bool {
61     val > std::u32::MAX
62 }
63
64 pub fn bar(len: u64) -> bool {
65     // This is OK as we are casting from target sized to fixed size
66     len >= std::usize::MAX as u64
67 }