diff --git a/clippy_config/src/conf.rs b/clippy_config/src/conf.rs index afb6d38072b3..f6ae98154dda 100644 --- a/clippy_config/src/conf.rs +++ b/clippy_config/src/conf.rs @@ -657,7 +657,7 @@ define_Conf! { /// x == VALUE /// } /// ``` - (float_cmp_allowed_constants: Vec = true), + (float_cmp_allowed_constants: Vec = Vec::new()), /// Lint: FLOAT_CMP /// /// Whether to ignore comparisons which have a constant result. diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index 25cd76104007..d284d1f4eeb6 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -816,7 +816,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) { store.register_late_pass(move |_| Box::new(manual_rem_euclid::ManualRemEuclid::new(conf))); store.register_late_pass(move |_| Box::new(manual_retain::ManualRetain::new(conf))); store.register_late_pass(move |_| Box::new(manual_rotate::ManualRotate)); - store.register_late_pass(move |_| Box::new(operators::Operators::new(conf))); + store.register_late_pass(move |tcx| Box::new(operators::Operators::new(tcx, conf))); store.register_late_pass(|_| Box::::default()); store.register_late_pass(move |_| Box::new(instant_subtraction::InstantSubtraction::new(conf))); store.register_late_pass(|_| Box::new(partialeq_to_none::PartialeqToNone)); diff --git a/clippy_lints/src/operators/float_cmp.rs b/clippy_lints/src/operators/float_cmp.rs index 986e221fca3a..6a7961d9b464 100644 --- a/clippy_lints/src/operators/float_cmp.rs +++ b/clippy_lints/src/operators/float_cmp.rs @@ -2,7 +2,7 @@ use clippy_utils::consts::{constant, Constant}; use clippy_utils::diagnostics::span_lint_and_then; use clippy_utils::sugg::Sugg; use clippy_utils::visitors::{for_each_expr_without_closures, is_const_evaluatable}; -use clippy_utils::{get_item_name, is_expr_named_const, path_res, peel_hir_expr_while, SpanlessEq}; +use clippy_utils::{get_item_name, get_named_const_def_id, path_res, peel_hir_expr_while, SpanlessEq}; use core::ops::ControlFlow; use rustc_errors::Applicability; use rustc_hir::def::Res; @@ -14,7 +14,7 @@ use super::{FloatCmpConfig, FLOAT_CMP}; pub(crate) fn check<'tcx>( cx: &LateContext<'tcx>, - config: FloatCmpConfig, + config: &FloatCmpConfig, expr: &'tcx Expr<'_>, op: BinOpKind, left: &'tcx Expr<'_>, @@ -56,8 +56,8 @@ pub(crate) fn check<'tcx>( return; } - if config.ignore_named_constants - && (is_expr_named_const(cx, left_reduced) || is_expr_named_const(cx, right_reduced)) + 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)) { return; } diff --git a/clippy_lints/src/operators/mod.rs b/clippy_lints/src/operators/mod.rs index e1193b587b1a..43488c6064d4 100644 --- a/clippy_lints/src/operators/mod.rs +++ b/clippy_lints/src/operators/mod.rs @@ -28,6 +28,7 @@ use clippy_utils::def_path_def_ids; use rustc_hir::def_id::DefIdSet; use rustc_hir::{Body, Expr, ExprKind, UnOp}; use rustc_lint::{LateContext, LateLintPass}; +use rustc_middle::ty::TyCtxt; use rustc_session::impl_lint_pass; declare_clippy_lint! { @@ -789,7 +790,7 @@ pub struct Operators { float_cmp_config: FloatCmpConfig, } impl Operators { - pub fn new(conf: &'static Conf) -> Self { + pub fn new(tcx: TyCtxt<'_>, conf: &'static Conf) -> Self { Self { arithmetic_context: numeric_arithmetic::Context::default(), verbose_bit_mask_threshold: conf.verbose_bit_mask_threshold, @@ -859,7 +860,7 @@ impl<'tcx> LateLintPass<'tcx> for Operators { float_equality_without_abs::check(cx, e, op.node, lhs, rhs); integer_division::check(cx, e, op.node, lhs, rhs); cmp_owned::check(cx, op.node, lhs, rhs); - float_cmp::check(cx, self.float_cmp_config, e, op.node, lhs, rhs); + float_cmp::check(cx, &self.float_cmp_config, e, op.node, lhs, rhs); modulo_one::check(cx, e, op.node, rhs); modulo_arithmetic::check( cx, diff --git a/clippy_utils/src/lib.rs b/clippy_utils/src/lib.rs index fb1493884fef..3bbf64708706 100644 --- a/clippy_utils/src/lib.rs +++ b/clippy_utils/src/lib.rs @@ -245,14 +245,15 @@ pub fn is_inside_always_const_context(tcx: TyCtxt<'_>, hir_id: HirId) -> bool { } } -/// Checks if the expression is path to either a constant or an associated constant. -pub fn is_expr_named_const<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -> bool { - matches!(&e.kind, ExprKind::Path(p) - if matches!( - cx.qpath_res(p, e.hir_id), - Res::Def(DefKind::Const | DefKind::AssocConst, _) - ) - ) +/// If the expression is path to either a constant or an associated constant get the `DefId`. +pub fn get_named_const_def_id<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -> Option { + if let ExprKind::Path(p) = &e.kind + && let Res::Def(DefKind::Const | DefKind::AssocConst, id) = cx.qpath_res(p, e.hir_id) + { + Some(id) + } else { + None + } } /// Checks if a `Res` refers to a constructor of a `LangItem` diff --git a/tests/ui-toml/float_cmp/change_detect/clippy.toml b/tests/ui-toml/float_cmp/change_detect/clippy.toml index c78367975726..65e32118cb09 100644 --- a/tests/ui-toml/float_cmp/change_detect/clippy.toml +++ b/tests/ui-toml/float_cmp/change_detect/clippy.toml @@ -1 +1,6 @@ float-cmp-ignore-change-detection = false +float-cmp-allowed-constants = [ + "core::f32::EPSILON", + "f32::EPSILON", + "test::F32_ARRAY", +] diff --git a/tests/ui-toml/float_cmp/const_cmp/clippy.toml b/tests/ui-toml/float_cmp/const_cmp/clippy.toml index 34006387a248..c7521206a899 100644 --- a/tests/ui-toml/float_cmp/const_cmp/clippy.toml +++ b/tests/ui-toml/float_cmp/const_cmp/clippy.toml @@ -1 +1,6 @@ float-cmp-ignore-constant-comparisons = false +float-cmp-allowed-constants = [ + "core::f32::EPSILON", + "f32::EPSILON", + "test::F32_ARRAY", +] diff --git a/tests/ui-toml/float_cmp/named_const/clippy.toml b/tests/ui-toml/float_cmp/named_const/clippy.toml index 7e24aab86cf6..e69de29bb2d1 100644 --- a/tests/ui-toml/float_cmp/named_const/clippy.toml +++ b/tests/ui-toml/float_cmp/named_const/clippy.toml @@ -1 +0,0 @@ -float-cmp-ignore-named-constants = false diff --git a/tests/ui-toml/float_cmp/test.change_detect.stderr b/tests/ui-toml/float_cmp/test.change_detect.stderr index e212739c7afe..397b035ca13f 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:15:21 + --> tests/ui-toml/float_cmp/test.rs:17:21 | LL | let _ = x == y; | ^^^^^^ help: consider comparing them within some margin of error: `(x - y).abs() < error_margin` @@ -11,130 +11,130 @@ LL | #![deny(clippy::float_cmp)] | ^^^^^^^^^^^^^^^^^ error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:16:21 + --> tests/ui-toml/float_cmp/test.rs:18: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:17:21 + --> tests/ui-toml/float_cmp/test.rs:19: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:18:21 + --> tests/ui-toml/float_cmp/test.rs:20: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:41:21 + --> tests/ui-toml/float_cmp/test.rs:43: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:42:21 + --> tests/ui-toml/float_cmp/test.rs:44: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:43:21 + --> tests/ui-toml/float_cmp/test.rs:45: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:44:21 + --> tests/ui-toml/float_cmp/test.rs:46: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:67:21 + --> tests/ui-toml/float_cmp/test.rs:69:21 | LL | let _ = x == y; | ^^^^^^ error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:68:21 + --> tests/ui-toml/float_cmp/test.rs:70:21 | LL | let _ = x == [5.5; 4]; | ^^^^^^^^^^^^^ error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:69:21 + --> tests/ui-toml/float_cmp/test.rs:71:21 | LL | let _ = [5.5; 4] == x; | ^^^^^^^^^^^^^ error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:70:21 + --> tests/ui-toml/float_cmp/test.rs:72: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:71:21 + --> tests/ui-toml/float_cmp/test.rs:73: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:87:21 + --> tests/ui-toml/float_cmp/test.rs:89:21 | LL | let _ = x == y; | ^^^^^^ error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:88:21 + --> tests/ui-toml/float_cmp/test.rs:90:21 | LL | let _ = x == [5.5; 4]; | ^^^^^^^^^^^^^ error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:89:21 + --> tests/ui-toml/float_cmp/test.rs:91:21 | LL | let _ = [5.5; 4] == x; | ^^^^^^^^^^^^^ error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:90:21 + --> tests/ui-toml/float_cmp/test.rs:92: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:91:21 + --> tests/ui-toml/float_cmp/test.rs:93: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:109:21 + --> tests/ui-toml/float_cmp/test.rs:111: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:115:21 + --> tests/ui-toml/float_cmp/test.rs:117:21 | LL | let _ = x == y; | ^^^^^^ error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:131:21 + --> tests/ui-toml/float_cmp/test.rs:132:21 | -LL | let _ = C * x == x * x; - | ^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(C * x - x * x).abs() < error_margin` +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:132:21 + --> tests/ui-toml/float_cmp/test.rs:133:21 | -LL | let _ = x * x == C * x; - | ^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(x * x - C * x).abs() < error_margin` +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 diff --git a/tests/ui-toml/float_cmp/test.const_cmp.stderr b/tests/ui-toml/float_cmp/test.const_cmp.stderr index 584b68caaae6..0335aa19d1eb 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:15:21 + --> tests/ui-toml/float_cmp/test.rs:17:21 | LL | let _ = x == y; | ^^^^^^ help: consider comparing them within some margin of error: `(x - y).abs() < error_margin` @@ -11,130 +11,130 @@ LL | #![deny(clippy::float_cmp)] | ^^^^^^^^^^^^^^^^^ error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:16:21 + --> tests/ui-toml/float_cmp/test.rs:18: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:17:21 + --> tests/ui-toml/float_cmp/test.rs:19: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:18:21 + --> tests/ui-toml/float_cmp/test.rs:20: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:41:21 + --> tests/ui-toml/float_cmp/test.rs:43: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:42:21 + --> tests/ui-toml/float_cmp/test.rs:44: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:43:21 + --> tests/ui-toml/float_cmp/test.rs:45: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:44:21 + --> tests/ui-toml/float_cmp/test.rs:46: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:67:21 + --> tests/ui-toml/float_cmp/test.rs:69:21 | LL | let _ = x == y; | ^^^^^^ error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:68:21 + --> tests/ui-toml/float_cmp/test.rs:70:21 | LL | let _ = x == [5.5; 4]; | ^^^^^^^^^^^^^ error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:69:21 + --> tests/ui-toml/float_cmp/test.rs:71:21 | LL | let _ = [5.5; 4] == x; | ^^^^^^^^^^^^^ error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:70:21 + --> tests/ui-toml/float_cmp/test.rs:72: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:71:21 + --> tests/ui-toml/float_cmp/test.rs:73: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:87:21 + --> tests/ui-toml/float_cmp/test.rs:89:21 | LL | let _ = x == y; | ^^^^^^ error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:88:21 + --> tests/ui-toml/float_cmp/test.rs:90:21 | LL | let _ = x == [5.5; 4]; | ^^^^^^^^^^^^^ error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:89:21 + --> tests/ui-toml/float_cmp/test.rs:91:21 | LL | let _ = [5.5; 4] == x; | ^^^^^^^^^^^^^ error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:90:21 + --> tests/ui-toml/float_cmp/test.rs:92: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:91:21 + --> tests/ui-toml/float_cmp/test.rs:93: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:109:21 + --> tests/ui-toml/float_cmp/test.rs:111: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:115:21 + --> tests/ui-toml/float_cmp/test.rs:117:21 | LL | let _ = x == y; | ^^^^^^ error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:131:21 + --> tests/ui-toml/float_cmp/test.rs:132:21 | -LL | let _ = C * x == x * x; - | ^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(C * x - x * x).abs() < error_margin` +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:132:21 + --> tests/ui-toml/float_cmp/test.rs:133:21 | -LL | let _ = x * x == C * x; - | ^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(x * x - C * x).abs() < error_margin` +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 diff --git a/tests/ui-toml/float_cmp/test.named_const.stderr b/tests/ui-toml/float_cmp/test.named_const.stderr index eb49aff7b280..7add667279cd 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:15:21 + --> tests/ui-toml/float_cmp/test.rs:17:21 | LL | let _ = x == y; | ^^^^^^ help: consider comparing them within some margin of error: `(x - y).abs() < error_margin` @@ -11,190 +11,190 @@ LL | #![deny(clippy::float_cmp)] | ^^^^^^^^^^^^^^^^^ error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:16:21 + --> tests/ui-toml/float_cmp/test.rs:18: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:17:21 + --> tests/ui-toml/float_cmp/test.rs:19: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:18:21 + --> tests/ui-toml/float_cmp/test.rs:20: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:41:21 + --> tests/ui-toml/float_cmp/test.rs:43: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:42:21 + --> tests/ui-toml/float_cmp/test.rs:44: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:43:21 + --> tests/ui-toml/float_cmp/test.rs:45: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:44:21 + --> tests/ui-toml/float_cmp/test.rs:46: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:67:21 + --> tests/ui-toml/float_cmp/test.rs:69:21 | LL | let _ = x == y; | ^^^^^^ error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:68:21 + --> tests/ui-toml/float_cmp/test.rs:70:21 | LL | let _ = x == [5.5; 4]; | ^^^^^^^^^^^^^ error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:69:21 + --> tests/ui-toml/float_cmp/test.rs:71:21 | LL | let _ = [5.5; 4] == x; | ^^^^^^^^^^^^^ error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:70:21 + --> tests/ui-toml/float_cmp/test.rs:72: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:71:21 + --> tests/ui-toml/float_cmp/test.rs:73: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:87:21 + --> tests/ui-toml/float_cmp/test.rs:89:21 | LL | let _ = x == y; | ^^^^^^ error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:88:21 + --> tests/ui-toml/float_cmp/test.rs:90:21 | LL | let _ = x == [5.5; 4]; | ^^^^^^^^^^^^^ error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:89:21 + --> tests/ui-toml/float_cmp/test.rs:91:21 | LL | let _ = [5.5; 4] == x; | ^^^^^^^^^^^^^ error: strict comparison of `f32` or `f64` arrays - --> tests/ui-toml/float_cmp/test.rs:90:21 + --> tests/ui-toml/float_cmp/test.rs:92: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:91:21 + --> tests/ui-toml/float_cmp/test.rs:93: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:109:21 + --> tests/ui-toml/float_cmp/test.rs:111: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:115:21 + --> tests/ui-toml/float_cmp/test.rs:117:21 | LL | let _ = x == y; | ^^^^^^ -error: strict comparison of `f32` or `f64` - --> tests/ui-toml/float_cmp/test.rs:124:21 - | -LL | let _ = x == C; - | ^^^^^^ help: consider comparing them within some margin of error: `(x - C).abs() < error_margin` - error: strict comparison of `f32` or `f64` --> tests/ui-toml/float_cmp/test.rs:125:21 | -LL | let _ = C == x; - | ^^^^^^ help: consider comparing them within some margin of error: `(C - x).abs() < error_margin` +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 | -LL | let _ = &&x == &&C; - | ^^^^^^^^^^ help: consider comparing them within some margin of error: `(&&x - &&C).abs() < error_margin` +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 | -LL | let _ = &&C == &&x; - | ^^^^^^^^^^ help: consider comparing them within some margin of error: `(&&C - &&x).abs() < error_margin` +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 | -LL | let _ = y == C as f64; - | ^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(y - C as f64).abs() < error_margin` +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 | -LL | let _ = C as f64 == y; - | ^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(C as f64 - y).abs() < error_margin` +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:131:21 + --> tests/ui-toml/float_cmp/test.rs:130:21 | -LL | let _ = C * x == x * x; - | ^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(C * x - x * x).abs() < error_margin` +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 | -LL | let _ = x * x == C * x; - | ^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(x * x - C * x).abs() < error_margin` +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 + | +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 | -LL | let _ = x == C; - | ^^^^^^ +LL | let _ = x == F32_ARRAY; + | ^^^^^^^^^^^^^^ error: strict comparison of `f32` or `f64` arrays --> tests/ui-toml/float_cmp/test.rs:139:21 | -LL | let _ = C == x; - | ^^^^^^ +LL | let _ = F32_ARRAY == x; + | ^^^^^^^^^^^^^^ error: strict comparison of `f32` or `f64` arrays --> tests/ui-toml/float_cmp/test.rs:140:21 | -LL | let _ = &&x == &&C; - | ^^^^^^^^^^ +LL | let _ = &&x == &&F32_ARRAY; + | ^^^^^^^^^^^^^^^^^^ error: strict comparison of `f32` or `f64` arrays --> tests/ui-toml/float_cmp/test.rs:141:21 | -LL | let _ = &&C == &&x; - | ^^^^^^^^^^ +LL | let _ = &&F32_ARRAY == &&x; + | ^^^^^^^^^^^^^^^^^^ error: strict comparison of `f32` or `f64` --> tests/ui-toml/float_cmp/test.rs:158:17 diff --git a/tests/ui-toml/float_cmp/test.rs b/tests/ui-toml/float_cmp/test.rs index 443064b45733..e9a084bd1f11 100644 --- a/tests/ui-toml/float_cmp/test.rs +++ b/tests/ui-toml/float_cmp/test.rs @@ -7,7 +7,9 @@ // 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)] +#![allow(clippy::op_ref, clippy::eq_op, clippy::legacy_numeric_constants)] + +const F32_ARRAY: [f32; 2] = [5.5, 5.5]; fn main() { { @@ -119,26 +121,24 @@ fn main() { // Comparisons to named constant { - const C: f32 = 5.5; fn _f(x: f32, y: f64) { - let _ = x == C; //~[named_const] float_cmp - let _ = C == x; //~[named_const] float_cmp - let _ = &&x == &&C; //~[named_const] float_cmp - let _ = &&C == &&x; //~[named_const] float_cmp - let _ = y == C as f64; //~[named_const] float_cmp - let _ = C as f64 == y; //~[named_const] float_cmp - - let _ = C * x == x * x; //~ float_cmp - let _ = x * x == C * x; //~ float_cmp + let _ = x == f32::EPSILON; //~[named_const] float_cmp + let _ = f32::EPSILON == x; //~[named_const] float_cmp + let _ = &&x == &&core::f32::EPSILON; //~[named_const] float_cmp + let _ = &&core::f32::EPSILON == &&x; //~[named_const] float_cmp + let _ = y == f32::EPSILON as f64; //~[named_const] float_cmp + let _ = f32::EPSILON as f64 == y; //~[named_const] float_cmp + + let _ = f32::EPSILON * x == x * x; //~ float_cmp + let _ = x * x == f32::EPSILON * x; //~ float_cmp } } { - const C: [f32; 2] = [5.5, 5.5]; fn _f(x: [f32; 2]) { - let _ = x == C; //~[named_const] float_cmp - let _ = C == x; //~[named_const] float_cmp - let _ = &&x == &&C; //~[named_const] float_cmp - let _ = &&C == &&x; //~[named_const] float_cmp + let _ = x == F32_ARRAY; //~[named_const] float_cmp + let _ = F32_ARRAY == x; //~[named_const] float_cmp + let _ = &&x == &&F32_ARRAY; //~[named_const] float_cmp + let _ = &&F32_ARRAY == &&x; //~[named_const] float_cmp } }