From f637c45f8b375606fc077b4a90ef4cb35758d219 Mon Sep 17 00:00:00 2001 From: Marcin Serwin Date: Mon, 6 Apr 2020 09:06:50 +0200 Subject: [PATCH] Indicate when arrays are compared in error message --- clippy_lints/src/misc.rs | 21 ++++++++++++++++++--- tests/ui/float_cmp.stderr | 2 +- tests/ui/float_cmp_const.stderr | 2 +- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/clippy_lints/src/misc.rs b/clippy_lints/src/misc.rs index 491cebc96ab..672fbd360d5 100644 --- a/clippy_lints/src/misc.rs +++ b/clippy_lints/src/misc.rs @@ -369,16 +369,31 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MiscLints { return; } } + let is_comparing_arrays = is_array(cx, left) || is_array(cx, right); let (lint, msg) = if is_named_constant(cx, left) || is_named_constant(cx, right) { - (FLOAT_CMP_CONST, "strict comparison of `f32` or `f64` constant") + ( + FLOAT_CMP_CONST, + if is_comparing_arrays { + "strict comparison of `f32` or `f64` constant arrays" + } else { + "strict comparison of `f32` or `f64` constant" + }, + ) } else { - (FLOAT_CMP, "strict comparison of `f32` or `f64`") + ( + FLOAT_CMP, + if is_comparing_arrays { + "strict comparison of `f32` or `f64` arrays" + } else { + "strict comparison of `f32` or `f64`" + }, + ) }; span_lint_and_then(cx, lint, expr.span, msg, |db| { let lhs = Sugg::hir(cx, left, ".."); let rhs = Sugg::hir(cx, right, ".."); - if !(is_array(cx, left) || is_array(cx, right)) { + if !is_comparing_arrays { db.span_suggestion( expr.span, "consider comparing them within some error", diff --git a/tests/ui/float_cmp.stderr b/tests/ui/float_cmp.stderr index 8952caa0676..8718cd83027 100644 --- a/tests/ui/float_cmp.stderr +++ b/tests/ui/float_cmp.stderr @@ -47,7 +47,7 @@ note: `f32::EPSILON` and `f64::EPSILON` are available. LL | NON_ZERO_ARRAY[i] == NON_ZERO_ARRAY[j]; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error: strict comparison of `f32` or `f64` +error: strict comparison of `f32` or `f64` arrays --> $DIR/float_cmp.rs:98:5 | LL | a1 == a2; diff --git a/tests/ui/float_cmp_const.stderr b/tests/ui/float_cmp_const.stderr index f93ee310785..5cdbc1d0013 100644 --- a/tests/ui/float_cmp_const.stderr +++ b/tests/ui/float_cmp_const.stderr @@ -83,7 +83,7 @@ note: `f32::EPSILON` and `f64::EPSILON` are available. LL | v != ONE; | ^^^^^^^^ -error: strict comparison of `f32` or `f64` constant +error: strict comparison of `f32` or `f64` constant arrays --> $DIR/float_cmp_const.rs:61:5 | LL | NON_ZERO_ARRAY == NON_ZERO_ARRAY2;