2024-09-23 15:26:43 +00:00
|
|
|
//@no-rustfix: suggestions have an error margin placeholder
|
2018-07-28 15:34:52 +00:00
|
|
|
#![warn(clippy::float_cmp_const)]
|
|
|
|
#![allow(clippy::float_cmp)]
|
|
|
|
#![allow(unused, clippy::no_effect, clippy::unnecessary_operation)]
|
2017-11-04 08:32:58 +00:00
|
|
|
|
|
|
|
const ONE: f32 = 1.0;
|
|
|
|
const TWO: f32 = 2.0;
|
|
|
|
|
|
|
|
fn eq_one(x: f32) -> bool {
|
2021-03-01 17:53:33 +00:00
|
|
|
if x.is_nan() { false } else { x == ONE } // no error, inside "eq" fn
|
2017-11-04 08:32:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
// has errors
|
|
|
|
1f32 == ONE;
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: strict comparison of `f32` or `f64` constant
|
2017-11-04 08:32:58 +00:00
|
|
|
TWO == ONE;
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: strict comparison of `f32` or `f64` constant
|
2017-11-04 08:32:58 +00:00
|
|
|
TWO != ONE;
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: strict comparison of `f32` or `f64` constant
|
2017-11-04 08:32:58 +00:00
|
|
|
ONE + ONE == TWO;
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: strict comparison of `f32` or `f64` constant
|
2019-03-12 07:49:26 +00:00
|
|
|
let x = 1;
|
|
|
|
x as f32 == ONE;
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: strict comparison of `f32` or `f64` constant
|
2017-11-04 08:32:58 +00:00
|
|
|
|
|
|
|
let v = 0.9;
|
|
|
|
v == ONE;
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: strict comparison of `f32` or `f64` constant
|
2017-11-04 08:32:58 +00:00
|
|
|
v != ONE;
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: strict comparison of `f32` or `f64` constant
|
2017-11-04 08:32:58 +00:00
|
|
|
|
|
|
|
// no errors, lower than or greater than comparisons
|
|
|
|
v < ONE;
|
|
|
|
v > ONE;
|
|
|
|
v <= ONE;
|
|
|
|
v >= ONE;
|
2017-11-06 09:02:42 +00:00
|
|
|
|
|
|
|
// no errors, zero and infinity values
|
|
|
|
ONE != 0f32;
|
|
|
|
TWO == 0f32;
|
2020-04-07 22:04:33 +00:00
|
|
|
ONE != f32::INFINITY;
|
|
|
|
ONE == f32::NEG_INFINITY;
|
2017-11-06 09:23:18 +00:00
|
|
|
|
2018-07-28 15:34:52 +00:00
|
|
|
// no errors, but will warn clippy::float_cmp if '#![allow(float_cmp)]' above is removed
|
2017-11-06 10:34:30 +00:00
|
|
|
let w = 1.1;
|
|
|
|
v == w;
|
|
|
|
v != w;
|
|
|
|
v == 1.0;
|
|
|
|
v != 1.0;
|
2020-03-20 10:54:04 +00:00
|
|
|
|
|
|
|
const ZERO_ARRAY: [f32; 3] = [0.0, 0.0, 0.0];
|
2020-10-24 23:21:40 +00:00
|
|
|
const ZERO_INF_ARRAY: [f32; 3] = [0.0, f32::INFINITY, f32::NEG_INFINITY];
|
2020-03-20 10:54:04 +00:00
|
|
|
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;
|
2023-07-28 19:35:48 +00:00
|
|
|
//~^ ERROR: strict comparison of `f32` or `f64` constant arrays
|
2017-11-04 08:32:58 +00:00
|
|
|
}
|