]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/float_cmp_const.rs
Rollup merge of #86828 - lambinoo:67441-const-fn-copied-take-replace, r=joshtriplett
[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() { false } else { x == ONE } // no error, inside "eq" fn
12 }
13
14 fn main() {
15     // has errors
16     1f32 == ONE;
17     TWO == ONE;
18     TWO != ONE;
19     ONE + ONE == TWO;
20     let x = 1;
21     x as f32 == ONE;
22
23     let v = 0.9;
24     v == ONE;
25     v != ONE;
26
27     // no errors, lower than or greater than comparisons
28     v < ONE;
29     v > ONE;
30     v <= ONE;
31     v >= ONE;
32
33     // no errors, zero and infinity values
34     ONE != 0f32;
35     TWO == 0f32;
36     ONE != f32::INFINITY;
37     ONE == f32::NEG_INFINITY;
38
39     // no errors, but will warn clippy::float_cmp if '#![allow(float_cmp)]' above is removed
40     let w = 1.1;
41     v == w;
42     v != w;
43     v == 1.0;
44     v != 1.0;
45
46     const ZERO_ARRAY: [f32; 3] = [0.0, 0.0, 0.0];
47     const ZERO_INF_ARRAY: [f32; 3] = [0.0, f32::INFINITY, f32::NEG_INFINITY];
48     const NON_ZERO_ARRAY: [f32; 3] = [0.0, 0.1, 0.2];
49     const NON_ZERO_ARRAY2: [f32; 3] = [0.2, 0.1, 0.0];
50
51     // no errors, zero and infinity values
52     NON_ZERO_ARRAY[0] == NON_ZERO_ARRAY2[1]; // lhs is 0.0
53     ZERO_ARRAY == NON_ZERO_ARRAY; // lhs is all zeros
54     ZERO_INF_ARRAY == NON_ZERO_ARRAY; // lhs is all zeros or infinities
55
56     // has errors
57     NON_ZERO_ARRAY == NON_ZERO_ARRAY2;
58 }