]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/float_cmp.rs
Rollup merge of #84221 - ABouttefeux:generic-arg-elision, r=estebank
[rust.git] / src / tools / clippy / tests / ui / float_cmp.rs
1 #![warn(clippy::float_cmp)]
2 #![allow(
3     unused,
4     clippy::no_effect,
5     clippy::op_ref,
6     clippy::unnecessary_operation,
7     clippy::cast_lossless,
8     clippy::many_single_char_names
9 )]
10
11 use std::ops::Add;
12
13 const ZERO: f32 = 0.0;
14 const ONE: f32 = ZERO + 1.0;
15
16 fn twice<T>(x: T) -> T
17 where
18     T: Add<T, Output = T> + Copy,
19 {
20     x + x
21 }
22
23 fn eq_fl(x: f32, y: f32) -> bool {
24     if x.is_nan() { y.is_nan() } else { x == y } // no error, inside "eq" fn
25 }
26
27 fn fl_eq(x: f32, y: f32) -> bool {
28     if x.is_nan() { y.is_nan() } else { x == y } // no error, inside "eq" fn
29 }
30
31 struct X {
32     val: f32,
33 }
34
35 impl PartialEq for X {
36     fn eq(&self, o: &X) -> bool {
37         if self.val.is_nan() {
38             o.val.is_nan()
39         } else {
40             self.val == o.val // no error, inside "eq" fn
41         }
42     }
43 }
44
45 fn main() {
46     ZERO == 0f32; //no error, comparison with zero is ok
47     1.0f32 != f32::INFINITY; // also comparison with infinity
48     1.0f32 != f32::NEG_INFINITY; // and negative infinity
49     ZERO == 0.0; //no error, comparison with zero is ok
50     ZERO + ZERO != 1.0; //no error, comparison with zero is ok
51
52     ONE == 1f32;
53     ONE == 1.0 + 0.0;
54     ONE + ONE == ZERO + ONE + ONE;
55     ONE != 2.0;
56     ONE != 0.0; // no error, comparison with zero is ok
57     twice(ONE) != ONE;
58     ONE as f64 != 2.0;
59     ONE as f64 != 0.0; // no error, comparison with zero is ok
60
61     let x: f64 = 1.0;
62
63     x == 1.0;
64     x != 0f64; // no error, comparison with zero is ok
65
66     twice(x) != twice(ONE as f64);
67
68     x < 0.0; // no errors, lower or greater comparisons need no fuzzyness
69     x > 0.0;
70     x <= 0.0;
71     x >= 0.0;
72
73     let xs: [f32; 1] = [0.0];
74     let a: *const f32 = xs.as_ptr();
75     let b: *const f32 = xs.as_ptr();
76
77     assert_eq!(a, b); // no errors
78
79     const ZERO_ARRAY: [f32; 2] = [0.0, 0.0];
80     const NON_ZERO_ARRAY: [f32; 2] = [0.0, 0.1];
81
82     let i = 0;
83     let j = 1;
84
85     ZERO_ARRAY[i] == NON_ZERO_ARRAY[j]; // ok, because lhs is zero regardless of i
86     NON_ZERO_ARRAY[i] == NON_ZERO_ARRAY[j];
87
88     let a1: [f32; 1] = [0.0];
89     let a2: [f32; 1] = [1.1];
90
91     a1 == a2;
92     a1[0] == a2[0];
93
94     // no errors - comparing signums is ok
95     let x32 = 3.21f32;
96     1.23f32.signum() == x32.signum();
97     1.23f32.signum() == -(x32.signum());
98     1.23f32.signum() == 3.21f32.signum();
99
100     1.23f32.signum() != x32.signum();
101     1.23f32.signum() != -(x32.signum());
102     1.23f32.signum() != 3.21f32.signum();
103
104     let x64 = 3.21f64;
105     1.23f64.signum() == x64.signum();
106     1.23f64.signum() == -(x64.signum());
107     1.23f64.signum() == 3.21f64.signum();
108
109     1.23f64.signum() != x64.signum();
110     1.23f64.signum() != -(x64.signum());
111     1.23f64.signum() != 3.21f64.signum();
112
113     // the comparison should also look through references
114     &0.0 == &ZERO;
115     &&&&0.0 == &&&&ZERO;
116 }