about summary refs log tree commit diff
path: root/src/tools/clippy/tests/ui/float_cmp_const.rs
blob: 47ea0e19c68b9c5ed4c31ab813dda8cbb23453d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// does not test any rustfixable lints
//@no-rustfix
#![warn(clippy::float_cmp_const)]
#![allow(clippy::float_cmp)]
#![allow(unused, clippy::no_effect, clippy::unnecessary_operation)]

const ONE: f32 = 1.0;
const TWO: f32 = 2.0;

fn eq_one(x: f32) -> bool {
    if x.is_nan() { false } else { x == ONE } // no error, inside "eq" fn
}

fn main() {
    // has errors
    1f32 == ONE;
    //~^ ERROR: strict comparison of `f32` or `f64` constant
    //~| NOTE: `f32::EPSILON` and `f64::EPSILON` are available for the `error_margin`
    TWO == ONE;
    //~^ ERROR: strict comparison of `f32` or `f64` constant
    //~| NOTE: `f32::EPSILON` and `f64::EPSILON` are available for the `error_margin`
    TWO != ONE;
    //~^ ERROR: strict comparison of `f32` or `f64` constant
    //~| NOTE: `f32::EPSILON` and `f64::EPSILON` are available for the `error_margin`
    ONE + ONE == TWO;
    //~^ ERROR: strict comparison of `f32` or `f64` constant
    //~| NOTE: `f32::EPSILON` and `f64::EPSILON` are available for the `error_margin`
    let x = 1;
    x as f32 == ONE;
    //~^ ERROR: strict comparison of `f32` or `f64` constant
    //~| NOTE: `f32::EPSILON` and `f64::EPSILON` are available for the `error_margin`

    let v = 0.9;
    v == ONE;
    //~^ ERROR: strict comparison of `f32` or `f64` constant
    //~| NOTE: `f32::EPSILON` and `f64::EPSILON` are available for the `error_margin`
    v != ONE;
    //~^ ERROR: strict comparison of `f32` or `f64` constant
    //~| NOTE: `f32::EPSILON` and `f64::EPSILON` are available for the `error_margin`

    // no errors, lower than or greater than comparisons
    v < ONE;
    v > ONE;
    v <= ONE;
    v >= ONE;

    // no errors, zero and infinity values
    ONE != 0f32;
    TWO == 0f32;
    ONE != f32::INFINITY;
    ONE == f32::NEG_INFINITY;

    // no errors, but will warn clippy::float_cmp if '#![allow(float_cmp)]' above is removed
    let w = 1.1;
    v == w;
    v != w;
    v == 1.0;
    v != 1.0;

    const ZERO_ARRAY: [f32; 3] = [0.0, 0.0, 0.0];
    const ZERO_INF_ARRAY: [f32; 3] = [0.0, f32::INFINITY, f32::NEG_INFINITY];
    const NON_ZERO_ARRAY: [f32; 3] = [0.0, 0.1, 0.2];
    const NON_ZERO_ARRAY2: [f32; 3] = [0.2, 0.1, 0.0];

    // no errors, zero and infinity values
    NON_ZERO_ARRAY[0] == NON_ZERO_ARRAY2[1]; // lhs is 0.0
    ZERO_ARRAY == NON_ZERO_ARRAY; // lhs is all zeros
    ZERO_INF_ARRAY == NON_ZERO_ARRAY; // lhs is all zeros or infinities

    // has errors
    NON_ZERO_ARRAY == NON_ZERO_ARRAY2;
    //~^ ERROR: strict comparison of `f32` or `f64` constant arrays
    //~| NOTE: `f32::EPSILON` and `f64::EPSILON` are available for the `error_margin`
}