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