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