From 40485461a652339110f5b900e800d28a3377ac9d Mon Sep 17 00:00:00 2001 From: Jason Newcomb Date: Thu, 18 Jul 2024 14:23:54 -0400 Subject: [PATCH] `float_cmp`: Allow literal to constant comparisons. --- clippy_lints/src/operators/float_cmp.rs | 11 ++- .../float_cmp/test.change_detect.stderr | 96 +++++++++---------- tests/ui-toml/float_cmp/test.const_cmp.stderr | 68 ++++++------- .../ui-toml/float_cmp/test.named_const.stderr | 82 ++++++++-------- tests/ui-toml/float_cmp/test.rs | 15 ++- 5 files changed, 145 insertions(+), 127 deletions(-) diff --git a/clippy_lints/src/operators/float_cmp.rs b/clippy_lints/src/operators/float_cmp.rs index 4b16c346ab02..5544a661e834 100644 --- a/clippy_lints/src/operators/float_cmp.rs +++ b/clippy_lints/src/operators/float_cmp.rs @@ -31,7 +31,9 @@ pub(crate) fn check<'tcx>( && is_float(cx, left_reduced) && is_float(cx, right_reduced) // Don't lint literal comparisons - && !(matches!(left_reduced.kind, ExprKind::Lit(_)) && matches!(right_reduced.kind, ExprKind::Lit(_))) + && let is_left_lit = matches!(left_reduced.kind, ExprKind::Lit(_)) + && let is_right_lit = matches!(right_reduced.kind, ExprKind::Lit(_)) + && !(is_left_lit && is_right_lit) // Allow comparing the results of signum() && !(is_signum(cx, left_reduced) && is_signum(cx, right_reduced)) && match (path_res(cx, left_reduced), path_res(cx, right_reduced)) { @@ -57,8 +59,11 @@ pub(crate) fn check<'tcx>( return; } - if get_named_const_def_id(cx, left_reduced).is_some_and(|id| config.allowed_constants.contains(&id)) - || get_named_const_def_id(cx, right_reduced).is_some_and(|id| config.allowed_constants.contains(&id)) + // Neither `CONST == lit` or `ALLOWED_CONST == x` should lint. + if get_named_const_def_id(cx, left_reduced) + .is_some_and(|id| is_right_lit || config.allowed_constants.contains(&id)) + || get_named_const_def_id(cx, right_reduced) + .is_some_and(|id| is_left_lit || config.allowed_constants.contains(&id)) { return; } diff --git a/tests/ui-toml/float_cmp/test.change_detect.stderr b/tests/ui-toml/float_cmp/test.change_detect.stderr index b83fa517b57c..49da8754d72f 100644 --- a/tests/ui-toml/float_cmp/test.change_detect.stderr +++ b/tests/ui-toml/float_cmp/test.change_detect.stderr @@ -1,5 +1,5 @@ error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:17:21 + --> tests/ui-toml/float_cmp/test.rs:22:21 | LL | let _ = x == y; | ^^^^^^ help: consider comparing them within some margin of error: `(x - y).abs() < error_margin` @@ -11,283 +11,283 @@ LL | #![deny(clippy::float_cmp)] | ^^^^^^^^^^^^^^^^^ error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:18:21 + --> tests/ui-toml/float_cmp/test.rs:23:21 | LL | let _ = x != y; | ^^^^^^ help: consider comparing them within some margin of error: `(x - y).abs() > error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:19:21 + --> tests/ui-toml/float_cmp/test.rs:24:21 | LL | let _ = x == 5.5; | ^^^^^^^^ help: consider comparing them within some margin of error: `(x - 5.5).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:20:21 + --> tests/ui-toml/float_cmp/test.rs:25:21 | LL | let _ = 5.5 == x; | ^^^^^^^^ help: consider comparing them within some margin of error: `(5.5 - x).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:43:21 + --> tests/ui-toml/float_cmp/test.rs:48:21 | LL | let _ = x == y; | ^^^^^^ help: consider comparing them within some margin of error: `(x - y).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:44:21 + --> tests/ui-toml/float_cmp/test.rs:49:21 | LL | let _ = x != y; | ^^^^^^ help: consider comparing them within some margin of error: `(x - y).abs() > error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:45:21 + --> tests/ui-toml/float_cmp/test.rs:50:21 | LL | let _ = x == 5.5; | ^^^^^^^^ help: consider comparing them within some margin of error: `(x - 5.5).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:46:21 + --> tests/ui-toml/float_cmp/test.rs:51:21 | LL | let _ = 5.5 == x; | ^^^^^^^^ help: consider comparing them within some margin of error: `(5.5 - x).abs() < error_margin` error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:69:21 + --> tests/ui-toml/float_cmp/test.rs:74:21 | LL | let _ = x == y; | ^^^^^^ error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:70:21 + --> tests/ui-toml/float_cmp/test.rs:75:21 | LL | let _ = x == [5.5; 4]; | ^^^^^^^^^^^^^ error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:71:21 + --> tests/ui-toml/float_cmp/test.rs:76:21 | LL | let _ = [5.5; 4] == x; | ^^^^^^^^^^^^^ error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:72:21 + --> tests/ui-toml/float_cmp/test.rs:77:21 | LL | let _ = [0.0, 0.0, 0.0, 5.5] == x; | ^^^^^^^^^^^^^^^^^^^^^^^^^ error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:73:21 + --> tests/ui-toml/float_cmp/test.rs:78:21 | LL | let _ = x == [0.0, 0.0, 0.0, 5.5]; | ^^^^^^^^^^^^^^^^^^^^^^^^^ error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:89:21 + --> tests/ui-toml/float_cmp/test.rs:94:21 | LL | let _ = x == y; | ^^^^^^ error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:90:21 + --> tests/ui-toml/float_cmp/test.rs:95:21 | LL | let _ = x == [5.5; 4]; | ^^^^^^^^^^^^^ error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:91:21 + --> tests/ui-toml/float_cmp/test.rs:96:21 | LL | let _ = [5.5; 4] == x; | ^^^^^^^^^^^^^ error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:92:21 + --> tests/ui-toml/float_cmp/test.rs:97:21 | LL | let _ = [0.0, 0.0, 0.0, 5.5] == x; | ^^^^^^^^^^^^^^^^^^^^^^^^^ error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:93:21 + --> tests/ui-toml/float_cmp/test.rs:98:21 | LL | let _ = x == [0.0, 0.0, 0.0, 5.5]; | ^^^^^^^^^^^^^^^^^^^^^^^^^ error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:111:21 + --> tests/ui-toml/float_cmp/test.rs:116:21 | LL | let _ = x == y; | ^^^^^^ help: consider comparing them within some margin of error: `(x - y).abs() < error_margin` error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:117:21 + --> tests/ui-toml/float_cmp/test.rs:122:21 | LL | let _ = x == y; | ^^^^^^ error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:132:21 + --> tests/ui-toml/float_cmp/test.rs:137:21 | LL | let _ = f32::EPSILON * x == x * x; | ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(f32::EPSILON * x - x * x).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:133:21 + --> tests/ui-toml/float_cmp/test.rs:138:21 | LL | let _ = x * x == f32::EPSILON * x; | ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(x * x - f32::EPSILON * x).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:158:17 + --> tests/ui-toml/float_cmp/test.rs:163:17 | LL | let _ = f(1.0) == f(5.0); | ^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(f(1.0) - f(5.0)).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:159:17 + --> tests/ui-toml/float_cmp/test.rs:164:17 | LL | let _ = 1.0 == f(5.0); | ^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(1.0 - f(5.0)).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:160:17 + --> tests/ui-toml/float_cmp/test.rs:165:17 | LL | let _ = f(1.0) + 1.0 != 5.0; | ^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(f(1.0) + 1.0 - 5.0).abs() > error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:202:21 + --> tests/ui-toml/float_cmp/test.rs:207:21 | LL | let _ = x == C[1]; | ^^^^^^^^^ help: consider comparing them within some margin of error: `(x - C[1]).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:203:21 + --> tests/ui-toml/float_cmp/test.rs:208:21 | LL | let _ = C[1] == x; | ^^^^^^^^^ help: consider comparing them within some margin of error: `(C[1] - x).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:268:21 + --> tests/ui-toml/float_cmp/test.rs:273:21 | LL | let _ = x == x + 1.0; | ^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(x - (x + 1.0)).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:269:21 + --> tests/ui-toml/float_cmp/test.rs:274:21 | LL | let _ = x + 1.0 == x; | ^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(x + 1.0 - x).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:270:21 + --> tests/ui-toml/float_cmp/test.rs:275:21 | LL | let _ = -x == -x + 1.0; | ^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(-x - (-x + 1.0)).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:271:21 + --> tests/ui-toml/float_cmp/test.rs:276:21 | LL | let _ = -x + 1.0 == -x; | ^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(-x + 1.0 - -x).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:272:21 + --> tests/ui-toml/float_cmp/test.rs:277:21 | LL | let _ = x == f1(x); | ^^^^^^^^^^ help: consider comparing them within some margin of error: `(x - f1(x)).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:273:21 + --> tests/ui-toml/float_cmp/test.rs:278:21 | LL | let _ = f1(x) == x; | ^^^^^^^^^^ help: consider comparing them within some margin of error: `(f1(x) - x).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:274:21 + --> tests/ui-toml/float_cmp/test.rs:279:21 | LL | let _ = x == f2(x, y); | ^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(x - f2(x, y)).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:275:21 + --> tests/ui-toml/float_cmp/test.rs:280:21 | LL | let _ = f2(x, y) == x; | ^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(f2(x, y) - x).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:276:21 + --> tests/ui-toml/float_cmp/test.rs:281:21 | LL | let _ = f1(f1(x)) == f1(x); | ^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(f1(f1(x)) - f1(x)).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:277:21 + --> tests/ui-toml/float_cmp/test.rs:282:21 | LL | let _ = f1(x) == f1(f1(x)); | ^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(f1(x) - f1(f1(x))).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:280:21 + --> tests/ui-toml/float_cmp/test.rs:285:21 | LL | let _ = z.0 == z.0 + 1.0; | ^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(z.0 - (z.0 + 1.0)).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:281:21 + --> tests/ui-toml/float_cmp/test.rs:286:21 | LL | let _ = z.0 + 1.0 == z.0; | ^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(z.0 + 1.0 - z.0).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:285:21 + --> tests/ui-toml/float_cmp/test.rs:290:21 | LL | let _ = *x + 1.0 == *x; | ^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(*x + 1.0 - *x).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:286:21 + --> tests/ui-toml/float_cmp/test.rs:291:21 | LL | let _ = *x == *x + 1.0; | ^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(*x - (*x + 1.0)).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:287:21 + --> tests/ui-toml/float_cmp/test.rs:292:21 | LL | let _ = *x == f1(*x); | ^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(*x - f1(*x)).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:288:21 + --> tests/ui-toml/float_cmp/test.rs:293:21 | LL | let _ = f1(*x) == *x; | ^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(f1(*x) - *x).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:293:21 + --> tests/ui-toml/float_cmp/test.rs:298:21 | LL | let _ = x.next().unwrap() == x.next().unwrap() + 1.0; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(x.next().unwrap() - (x.next().unwrap() + 1.0)).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:309:21 + --> tests/ui-toml/float_cmp/test.rs:314:21 | LL | let _ = x.f() + 1.0 == x.f(); | ^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(x.f() + 1.0 - x.f()).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:310:21 + --> tests/ui-toml/float_cmp/test.rs:315:21 | LL | let _ = x.f() == x.f() + 1.0; | ^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(x.f() - (x.f() + 1.0)).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:315:17 + --> tests/ui-toml/float_cmp/test.rs:320:17 | LL | let _ = f(1.0) == f(1.0) + 1.0; | ^^^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(f(1.0) - (f(1.0) + 1.0)).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:319:17 + --> tests/ui-toml/float_cmp/test.rs:324:17 | LL | let _ = f(1.0) == f(1.0) + 1.0; | ^^^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(f(1.0) - (f(1.0) + 1.0)).abs() < error_margin` diff --git a/tests/ui-toml/float_cmp/test.const_cmp.stderr b/tests/ui-toml/float_cmp/test.const_cmp.stderr index d1dd285f2a08..cf50d04eab7d 100644 --- a/tests/ui-toml/float_cmp/test.const_cmp.stderr +++ b/tests/ui-toml/float_cmp/test.const_cmp.stderr @@ -1,5 +1,5 @@ error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:17:21 + --> tests/ui-toml/float_cmp/test.rs:22:21 | LL | let _ = x == y; | ^^^^^^ help: consider comparing them within some margin of error: `(x - y).abs() < error_margin` @@ -11,199 +11,199 @@ LL | #![deny(clippy::float_cmp)] | ^^^^^^^^^^^^^^^^^ error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:18:21 + --> tests/ui-toml/float_cmp/test.rs:23:21 | LL | let _ = x != y; | ^^^^^^ help: consider comparing them within some margin of error: `(x - y).abs() > error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:19:21 + --> tests/ui-toml/float_cmp/test.rs:24:21 | LL | let _ = x == 5.5; | ^^^^^^^^ help: consider comparing them within some margin of error: `(x - 5.5).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:20:21 + --> tests/ui-toml/float_cmp/test.rs:25:21 | LL | let _ = 5.5 == x; | ^^^^^^^^ help: consider comparing them within some margin of error: `(5.5 - x).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:43:21 + --> tests/ui-toml/float_cmp/test.rs:48:21 | LL | let _ = x == y; | ^^^^^^ help: consider comparing them within some margin of error: `(x - y).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:44:21 + --> tests/ui-toml/float_cmp/test.rs:49:21 | LL | let _ = x != y; | ^^^^^^ help: consider comparing them within some margin of error: `(x - y).abs() > error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:45:21 + --> tests/ui-toml/float_cmp/test.rs:50:21 | LL | let _ = x == 5.5; | ^^^^^^^^ help: consider comparing them within some margin of error: `(x - 5.5).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:46:21 + --> tests/ui-toml/float_cmp/test.rs:51:21 | LL | let _ = 5.5 == x; | ^^^^^^^^ help: consider comparing them within some margin of error: `(5.5 - x).abs() < error_margin` error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:69:21 + --> tests/ui-toml/float_cmp/test.rs:74:21 | LL | let _ = x == y; | ^^^^^^ error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:70:21 + --> tests/ui-toml/float_cmp/test.rs:75:21 | LL | let _ = x == [5.5; 4]; | ^^^^^^^^^^^^^ error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:71:21 + --> tests/ui-toml/float_cmp/test.rs:76:21 | LL | let _ = [5.5; 4] == x; | ^^^^^^^^^^^^^ error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:72:21 + --> tests/ui-toml/float_cmp/test.rs:77:21 | LL | let _ = [0.0, 0.0, 0.0, 5.5] == x; | ^^^^^^^^^^^^^^^^^^^^^^^^^ error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:73:21 + --> tests/ui-toml/float_cmp/test.rs:78:21 | LL | let _ = x == [0.0, 0.0, 0.0, 5.5]; | ^^^^^^^^^^^^^^^^^^^^^^^^^ error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:89:21 + --> tests/ui-toml/float_cmp/test.rs:94:21 | LL | let _ = x == y; | ^^^^^^ error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:90:21 + --> tests/ui-toml/float_cmp/test.rs:95:21 | LL | let _ = x == [5.5; 4]; | ^^^^^^^^^^^^^ error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:91:21 + --> tests/ui-toml/float_cmp/test.rs:96:21 | LL | let _ = [5.5; 4] == x; | ^^^^^^^^^^^^^ error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:92:21 + --> tests/ui-toml/float_cmp/test.rs:97:21 | LL | let _ = [0.0, 0.0, 0.0, 5.5] == x; | ^^^^^^^^^^^^^^^^^^^^^^^^^ error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:93:21 + --> tests/ui-toml/float_cmp/test.rs:98:21 | LL | let _ = x == [0.0, 0.0, 0.0, 5.5]; | ^^^^^^^^^^^^^^^^^^^^^^^^^ error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:111:21 + --> tests/ui-toml/float_cmp/test.rs:116:21 | LL | let _ = x == y; | ^^^^^^ help: consider comparing them within some margin of error: `(x - y).abs() < error_margin` error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:117:21 + --> tests/ui-toml/float_cmp/test.rs:122:21 | LL | let _ = x == y; | ^^^^^^ error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:132:21 + --> tests/ui-toml/float_cmp/test.rs:137:21 | LL | let _ = f32::EPSILON * x == x * x; | ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(f32::EPSILON * x - x * x).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:133:21 + --> tests/ui-toml/float_cmp/test.rs:138:21 | LL | let _ = x * x == f32::EPSILON * x; | ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(x * x - f32::EPSILON * x).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:150:17 + --> tests/ui-toml/float_cmp/test.rs:155:17 | LL | let _ = f(1.0) == f(5.0); | ^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(f(1.0) - f(5.0)).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:151:17 + --> tests/ui-toml/float_cmp/test.rs:156:17 | LL | let _ = 1.0 == f(5.0); | ^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(1.0 - f(5.0)).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:152:17 + --> tests/ui-toml/float_cmp/test.rs:157:17 | LL | let _ = f(1.0) + 1.0 != 5.0; | ^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(f(1.0) + 1.0 - 5.0).abs() > error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:158:17 + --> tests/ui-toml/float_cmp/test.rs:163:17 | LL | let _ = f(1.0) == f(5.0); | ^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(f(1.0) - f(5.0)).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:159:17 + --> tests/ui-toml/float_cmp/test.rs:164:17 | LL | let _ = 1.0 == f(5.0); | ^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(1.0 - f(5.0)).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:160:17 + --> tests/ui-toml/float_cmp/test.rs:165:17 | LL | let _ = f(1.0) + 1.0 != 5.0; | ^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(f(1.0) + 1.0 - 5.0).abs() > error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:202:21 + --> tests/ui-toml/float_cmp/test.rs:207:21 | LL | let _ = x == C[1]; | ^^^^^^^^^ help: consider comparing them within some margin of error: `(x - C[1]).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:203:21 + --> tests/ui-toml/float_cmp/test.rs:208:21 | LL | let _ = C[1] == x; | ^^^^^^^^^ help: consider comparing them within some margin of error: `(C[1] - x).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:293:21 + --> tests/ui-toml/float_cmp/test.rs:298:21 | LL | let _ = x.next().unwrap() == x.next().unwrap() + 1.0; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(x.next().unwrap() - (x.next().unwrap() + 1.0)).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:309:21 + --> tests/ui-toml/float_cmp/test.rs:314:21 | LL | let _ = x.f() + 1.0 == x.f(); | ^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(x.f() + 1.0 - x.f()).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:310:21 + --> tests/ui-toml/float_cmp/test.rs:315:21 | LL | let _ = x.f() == x.f() + 1.0; | ^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(x.f() - (x.f() + 1.0)).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:319:17 + --> tests/ui-toml/float_cmp/test.rs:324:17 | LL | let _ = f(1.0) == f(1.0) + 1.0; | ^^^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(f(1.0) - (f(1.0) + 1.0)).abs() < error_margin` diff --git a/tests/ui-toml/float_cmp/test.named_const.stderr b/tests/ui-toml/float_cmp/test.named_const.stderr index 2c2d70498167..20f041ec61c9 100644 --- a/tests/ui-toml/float_cmp/test.named_const.stderr +++ b/tests/ui-toml/float_cmp/test.named_const.stderr @@ -1,5 +1,5 @@ error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:17:21 + --> tests/ui-toml/float_cmp/test.rs:22:21 | LL | let _ = x == y; | ^^^^^^ help: consider comparing them within some margin of error: `(x - y).abs() < error_margin` @@ -11,241 +11,241 @@ LL | #![deny(clippy::float_cmp)] | ^^^^^^^^^^^^^^^^^ error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:18:21 + --> tests/ui-toml/float_cmp/test.rs:23:21 | LL | let _ = x != y; | ^^^^^^ help: consider comparing them within some margin of error: `(x - y).abs() > error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:19:21 + --> tests/ui-toml/float_cmp/test.rs:24:21 | LL | let _ = x == 5.5; | ^^^^^^^^ help: consider comparing them within some margin of error: `(x - 5.5).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:20:21 + --> tests/ui-toml/float_cmp/test.rs:25:21 | LL | let _ = 5.5 == x; | ^^^^^^^^ help: consider comparing them within some margin of error: `(5.5 - x).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:43:21 + --> tests/ui-toml/float_cmp/test.rs:48:21 | LL | let _ = x == y; | ^^^^^^ help: consider comparing them within some margin of error: `(x - y).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:44:21 + --> tests/ui-toml/float_cmp/test.rs:49:21 | LL | let _ = x != y; | ^^^^^^ help: consider comparing them within some margin of error: `(x - y).abs() > error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:45:21 + --> tests/ui-toml/float_cmp/test.rs:50:21 | LL | let _ = x == 5.5; | ^^^^^^^^ help: consider comparing them within some margin of error: `(x - 5.5).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:46:21 + --> tests/ui-toml/float_cmp/test.rs:51:21 | LL | let _ = 5.5 == x; | ^^^^^^^^ help: consider comparing them within some margin of error: `(5.5 - x).abs() < error_margin` error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:69:21 + --> tests/ui-toml/float_cmp/test.rs:74:21 | LL | let _ = x == y; | ^^^^^^ error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:70:21 + --> tests/ui-toml/float_cmp/test.rs:75:21 | LL | let _ = x == [5.5; 4]; | ^^^^^^^^^^^^^ error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:71:21 + --> tests/ui-toml/float_cmp/test.rs:76:21 | LL | let _ = [5.5; 4] == x; | ^^^^^^^^^^^^^ error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:72:21 + --> tests/ui-toml/float_cmp/test.rs:77:21 | LL | let _ = [0.0, 0.0, 0.0, 5.5] == x; | ^^^^^^^^^^^^^^^^^^^^^^^^^ error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:73:21 + --> tests/ui-toml/float_cmp/test.rs:78:21 | LL | let _ = x == [0.0, 0.0, 0.0, 5.5]; | ^^^^^^^^^^^^^^^^^^^^^^^^^ error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:89:21 + --> tests/ui-toml/float_cmp/test.rs:94:21 | LL | let _ = x == y; | ^^^^^^ error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:90:21 + --> tests/ui-toml/float_cmp/test.rs:95:21 | LL | let _ = x == [5.5; 4]; | ^^^^^^^^^^^^^ error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:91:21 + --> tests/ui-toml/float_cmp/test.rs:96:21 | LL | let _ = [5.5; 4] == x; | ^^^^^^^^^^^^^ error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:92:21 + --> tests/ui-toml/float_cmp/test.rs:97:21 | LL | let _ = [0.0, 0.0, 0.0, 5.5] == x; | ^^^^^^^^^^^^^^^^^^^^^^^^^ error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:93:21 + --> tests/ui-toml/float_cmp/test.rs:98:21 | LL | let _ = x == [0.0, 0.0, 0.0, 5.5]; | ^^^^^^^^^^^^^^^^^^^^^^^^^ error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:111:21 + --> tests/ui-toml/float_cmp/test.rs:116:21 | LL | let _ = x == y; | ^^^^^^ help: consider comparing them within some margin of error: `(x - y).abs() < error_margin` error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:117:21 + --> tests/ui-toml/float_cmp/test.rs:122:21 | LL | let _ = x == y; | ^^^^^^ error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:125:21 + --> tests/ui-toml/float_cmp/test.rs:130:21 | LL | let _ = x == f32::EPSILON; | ^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(x - f32::EPSILON).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:126:21 + --> tests/ui-toml/float_cmp/test.rs:131:21 | LL | let _ = f32::EPSILON == x; | ^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(f32::EPSILON - x).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:127:21 + --> tests/ui-toml/float_cmp/test.rs:132:21 | LL | let _ = &&x == &&core::f32::EPSILON; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(&&x - &&core::f32::EPSILON).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:128:21 + --> tests/ui-toml/float_cmp/test.rs:133:21 | LL | let _ = &&core::f32::EPSILON == &&x; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(&&core::f32::EPSILON - &&x).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:129:21 + --> tests/ui-toml/float_cmp/test.rs:134:21 | LL | let _ = y == f32::EPSILON as f64; | ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(y - f32::EPSILON as f64).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:130:21 + --> tests/ui-toml/float_cmp/test.rs:135:21 | LL | let _ = f32::EPSILON as f64 == y; | ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(f32::EPSILON as f64 - y).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:132:21 + --> tests/ui-toml/float_cmp/test.rs:137:21 | LL | let _ = f32::EPSILON * x == x * x; | ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(f32::EPSILON * x - x * x).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:133:21 + --> tests/ui-toml/float_cmp/test.rs:138:21 | LL | let _ = x * x == f32::EPSILON * x; | ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(x * x - f32::EPSILON * x).abs() < error_margin` error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:138:21 + --> tests/ui-toml/float_cmp/test.rs:143:21 | LL | let _ = x == F32_ARRAY; | ^^^^^^^^^^^^^^ error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:139:21 + --> tests/ui-toml/float_cmp/test.rs:144:21 | LL | let _ = F32_ARRAY == x; | ^^^^^^^^^^^^^^ error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:140:21 + --> tests/ui-toml/float_cmp/test.rs:145:21 | LL | let _ = &&x == &&F32_ARRAY; | ^^^^^^^^^^^^^^^^^^ error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:141:21 + --> tests/ui-toml/float_cmp/test.rs:146:21 | LL | let _ = &&F32_ARRAY == &&x; | ^^^^^^^^^^^^^^^^^^ error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:158:17 + --> tests/ui-toml/float_cmp/test.rs:163:17 | LL | let _ = f(1.0) == f(5.0); | ^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(f(1.0) - f(5.0)).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:159:17 + --> tests/ui-toml/float_cmp/test.rs:164:17 | LL | let _ = 1.0 == f(5.0); | ^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(1.0 - f(5.0)).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:160:17 + --> tests/ui-toml/float_cmp/test.rs:165:17 | LL | let _ = f(1.0) + 1.0 != 5.0; | ^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(f(1.0) + 1.0 - 5.0).abs() > error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:202:21 + --> tests/ui-toml/float_cmp/test.rs:207:21 | LL | let _ = x == C[1]; | ^^^^^^^^^ help: consider comparing them within some margin of error: `(x - C[1]).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:203:21 + --> tests/ui-toml/float_cmp/test.rs:208:21 | LL | let _ = C[1] == x; | ^^^^^^^^^ help: consider comparing them within some margin of error: `(C[1] - x).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:293:21 + --> tests/ui-toml/float_cmp/test.rs:298:21 | LL | let _ = x.next().unwrap() == x.next().unwrap() + 1.0; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(x.next().unwrap() - (x.next().unwrap() + 1.0)).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:309:21 + --> tests/ui-toml/float_cmp/test.rs:314:21 | LL | let _ = x.f() + 1.0 == x.f(); | ^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(x.f() + 1.0 - x.f()).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:310:21 + --> tests/ui-toml/float_cmp/test.rs:315:21 | LL | let _ = x.f() == x.f() + 1.0; | ^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(x.f() - (x.f() + 1.0)).abs() < error_margin` error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:319:17 + --> tests/ui-toml/float_cmp/test.rs:324:17 | LL | let _ = f(1.0) == f(1.0) + 1.0; | ^^^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(f(1.0) - (f(1.0) + 1.0)).abs() < error_margin` diff --git a/tests/ui-toml/float_cmp/test.rs b/tests/ui-toml/float_cmp/test.rs index e28b94aef739..75d8d43a684c 100644 --- a/tests/ui-toml/float_cmp/test.rs +++ b/tests/ui-toml/float_cmp/test.rs @@ -7,7 +7,12 @@ // FIXME(f16_f128): const casting is not yet supported for these types. Add when available. #![deny(clippy::float_cmp)] -#![allow(clippy::op_ref, clippy::eq_op, clippy::legacy_numeric_constants)] +#![allow( + clippy::op_ref, + clippy::eq_op, + clippy::legacy_numeric_constants, + clippy::excessive_precision +)] const F32_ARRAY: [f32; 2] = [5.5, 5.5]; @@ -330,4 +335,12 @@ fn main() { let _ = &&x == &&x; } } + + // Constant to literal + { + fn _f(x: f64) { + let _ = f64::EPSILON == 2.2204460492503131E-16f64; + let _ = 2.2204460492503131E-16f64 == core::f64::EPSILON; + } + } }