diff --git a/clippy_lints/src/operators/float_cmp.rs b/clippy_lints/src/operators/float_cmp.rs index a123636ba0c8..90789671e9f0 100644 --- a/clippy_lints/src/operators/float_cmp.rs +++ b/clippy_lints/src/operators/float_cmp.rs @@ -2,9 +2,10 @@ 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, is_const_evaluatable}; -use clippy_utils::{get_item_name, is_expr_named_const, peel_hir_expr_while, SpanlessEq}; +use clippy_utils::{get_item_name, is_expr_named_const, path_res, peel_hir_expr_while, SpanlessEq}; use core::ops::ControlFlow; use rustc_errors::Applicability; +use rustc_hir::def::Res; use rustc_hir::{BinOpKind, BorrowKind, Expr, ExprKind, UnOp, Unsafety}; use rustc_lint::LateContext; use rustc_middle::ty::{self, Ty, TypeFlags, TypeVisitableExt}; @@ -32,6 +33,10 @@ pub(crate) fn check<'tcx>( && !(matches!(left_red.kind, ExprKind::Lit(_)) && matches!(right_red.kind, ExprKind::Lit(_))) // Allow comparing the results of signum() && !(is_signum(cx, left_red) && is_signum(cx, right_red)) + && match (path_res(cx, left_red), path_res(cx, right_red)) { + (Res::Err, _) | (_, Res::Err) => true, + (left, right) => left != right, + } { let left_c = constant(cx, cx.typeck_results(), left_red); let is_left_const = left_c.is_some(); @@ -64,7 +69,7 @@ pub(crate) fn check<'tcx>( if let Some(name) = get_item_name(cx, expr) { let name = name.as_str(); - if name == "eq" || name == "ne" || name == "is_nan" || name.starts_with("eq_") || name.ends_with("_eq") { + if name == "eq" || name == "ne" || name.starts_with("eq_") || name.ends_with("_eq") { return; } } diff --git a/tests/ui-toml/float_cmp_change_detection/test.rs b/tests/ui-toml/float_cmp_change_detection/test.rs index 65f6b9edecc3..c16776e104e3 100644 --- a/tests/ui-toml/float_cmp_change_detection/test.rs +++ b/tests/ui-toml/float_cmp_change_detection/test.rs @@ -1,6 +1,7 @@ //@no-rustfix #![deny(clippy::float_cmp)] +#![allow(clippy::op_ref, clippy::eq_op)] fn main() { { @@ -26,4 +27,14 @@ fn main() { let _ = x == x + 1.0; } } + { + fn _f(x: f32) { + let _ = x == x; + let _ = x != x; + let _ = x == -x; + let _ = -x == x; + let _ = x as f64 == x as f64; + let _ = &&x == &&x; + } + } } diff --git a/tests/ui-toml/float_cmp_change_detection/test.stderr b/tests/ui-toml/float_cmp_change_detection/test.stderr index 6ee387416288..d329793ba2a8 100644 --- a/tests/ui-toml/float_cmp_change_detection/test.stderr +++ b/tests/ui-toml/float_cmp_change_detection/test.stderr @@ -1,5 +1,5 @@ error: strict comparison of `f32` or `f64` - --> $DIR/test.rs:25:21 + --> $DIR/test.rs:26:21 | LL | let _ = x + 1.0 == x; | ^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(x + 1.0 - x).abs() < error_margin` @@ -11,7 +11,7 @@ LL | #![deny(clippy::float_cmp)] | ^^^^^^^^^^^^^^^^^ error: strict comparison of `f32` or `f64` - --> $DIR/test.rs:26:21 + --> $DIR/test.rs:27:21 | LL | let _ = x == x + 1.0; | ^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(x - (x + 1.0)).abs() < error_margin` diff --git a/tests/ui-toml/float_cmp_constant_comparisons/test.rs b/tests/ui-toml/float_cmp_constant_comparisons/test.rs index 65f6b9edecc3..c16776e104e3 100644 --- a/tests/ui-toml/float_cmp_constant_comparisons/test.rs +++ b/tests/ui-toml/float_cmp_constant_comparisons/test.rs @@ -1,6 +1,7 @@ //@no-rustfix #![deny(clippy::float_cmp)] +#![allow(clippy::op_ref, clippy::eq_op)] fn main() { { @@ -26,4 +27,14 @@ fn main() { let _ = x == x + 1.0; } } + { + fn _f(x: f32) { + let _ = x == x; + let _ = x != x; + let _ = x == -x; + let _ = -x == x; + let _ = x as f64 == x as f64; + let _ = &&x == &&x; + } + } } diff --git a/tests/ui-toml/float_cmp_constant_comparisons/test.stderr b/tests/ui-toml/float_cmp_constant_comparisons/test.stderr index 3ab8b23bb6dc..01c21ebdadff 100644 --- a/tests/ui-toml/float_cmp_constant_comparisons/test.stderr +++ b/tests/ui-toml/float_cmp_constant_comparisons/test.stderr @@ -1,5 +1,5 @@ error: strict comparison of `f32` or `f64` - --> $DIR/test.rs:16:17 + --> $DIR/test.rs:17:17 | LL | let _ = f(1.0) == f(2.0); | ^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(f(1.0) - f(2.0)).abs() < error_margin` diff --git a/tests/ui-toml/float_cmp_named_constants/test.rs b/tests/ui-toml/float_cmp_named_constants/test.rs index 65f6b9edecc3..c16776e104e3 100644 --- a/tests/ui-toml/float_cmp_named_constants/test.rs +++ b/tests/ui-toml/float_cmp_named_constants/test.rs @@ -1,6 +1,7 @@ //@no-rustfix #![deny(clippy::float_cmp)] +#![allow(clippy::op_ref, clippy::eq_op)] fn main() { { @@ -26,4 +27,14 @@ fn main() { let _ = x == x + 1.0; } } + { + fn _f(x: f32) { + let _ = x == x; + let _ = x != x; + let _ = x == -x; + let _ = -x == x; + let _ = x as f64 == x as f64; + let _ = &&x == &&x; + } + } } diff --git a/tests/ui-toml/float_cmp_named_constants/test.stderr b/tests/ui-toml/float_cmp_named_constants/test.stderr index 617d95d4ed67..327a6634ef23 100644 --- a/tests/ui-toml/float_cmp_named_constants/test.stderr +++ b/tests/ui-toml/float_cmp_named_constants/test.stderr @@ -1,5 +1,5 @@ error: strict comparison of `f32` or `f64` - --> $DIR/test.rs:9:21 + --> $DIR/test.rs:10:21 | LL | let _ = x == C; | ^^^^^^ help: consider comparing them within some margin of error: `(x - C).abs() < error_margin` diff --git a/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr b/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr index 0bc6dca2f0c0..e2aa2bb72af8 100644 --- a/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr +++ b/tests/ui-toml/toml_unknown_key/conf_unknown_key.stderr @@ -202,6 +202,9 @@ error: error reading Clippy's configuration file: unknown field `allow_mixed_uni enum-variant-name-threshold enum-variant-size-threshold excessive-nesting-threshold + float-cmp-ignore-change-detection + float-cmp-ignore-constant-comparisons + float-cmp-ignore-named-constants future-size-threshold ignore-interior-mutability large-error-threshold diff --git a/tests/ui/float_cmp.rs b/tests/ui/float_cmp.rs index 9798a20565c2..5a6d20c464b2 100644 --- a/tests/ui/float_cmp.rs +++ b/tests/ui/float_cmp.rs @@ -1,7 +1,7 @@ //@no-rustfix #![warn(clippy::float_cmp)] -#![allow(clippy::op_ref)] +#![allow(clippy::op_ref, clippy::eq_op)] fn main() { { @@ -330,4 +330,16 @@ fn main() { //~^ ERROR: strict comparison of `f32` or `f64` } } + + // Self comparisons + { + fn _f(x: f32) { + let _ = x == x; + let _ = x != x; + let _ = x == -x; + let _ = -x == x; + let _ = x as f64 == x as f64; + let _ = &&x == &&x; + } + } }