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