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