]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/float_cmp_const.rs
dfc025558a2f430b35b1624b6cd37c5263dc76c4
[rust.git] / src / tools / clippy / tests / ui / float_cmp_const.rs
1 // does not test any rustfixable lints
2
3 #![warn(clippy::float_cmp_const)]
4 #![allow(clippy::float_cmp)]
5 #![allow(unused, clippy::no_effect, clippy::unnecessary_operation)]
6
7 const ONE: f32 = 1.0;
8 const TWO: f32 = 2.0;
9
10 fn eq_one(x: f32) -> bool {
11     if x.is_nan() {
12         false
13     } else {
14         x == ONE
15     } // no error, inside "eq" fn
16 }
17
18 fn main() {
19     // has errors
20     1f32 == ONE;
21     TWO == ONE;
22     TWO != ONE;
23     ONE + ONE == TWO;
24     let x = 1;
25     x as f32 == ONE;
26
27     let v = 0.9;
28     v == ONE;
29     v != ONE;
30
31     // no errors, lower than or greater than comparisons
32     v < ONE;
33     v > ONE;
34     v <= ONE;
35     v >= ONE;
36
37     // no errors, zero and infinity values
38     ONE != 0f32;
39     TWO == 0f32;
40     ONE != f32::INFINITY;
41     ONE == f32::NEG_INFINITY;
42
43     // no errors, but will warn clippy::float_cmp if '#![allow(float_cmp)]' above is removed
44     let w = 1.1;
45     v == w;
46     v != w;
47     v == 1.0;
48     v != 1.0;
49
50     const ZERO_ARRAY: [f32; 3] = [0.0, 0.0, 0.0];
51     const ZERO_INF_ARRAY: [f32; 3] = [0.0, ::std::f32::INFINITY, ::std::f32::NEG_INFINITY];
52     const NON_ZERO_ARRAY: [f32; 3] = [0.0, 0.1, 0.2];
53     const NON_ZERO_ARRAY2: [f32; 3] = [0.2, 0.1, 0.0];
54
55     // no errors, zero and infinity values
56     NON_ZERO_ARRAY[0] == NON_ZERO_ARRAY2[1]; // lhs is 0.0
57     ZERO_ARRAY == NON_ZERO_ARRAY; // lhs is all zeros
58     ZERO_INF_ARRAY == NON_ZERO_ARRAY; // lhs is all zeros or infinities
59
60     // has errors
61     NON_ZERO_ARRAY == NON_ZERO_ARRAY2;
62 }