Make eq_op suggest .is_nan()

This commit is contained in:
Catherine 2023-06-30 11:39:33 -05:00
parent 2c40b99d08
commit efac83813d
3 changed files with 41 additions and 26 deletions

View File

@ -1,4 +1,4 @@
use clippy_utils::diagnostics::span_lint; use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
use clippy_utils::macros::{find_assert_eq_args, first_node_macro_backtrace}; use clippy_utils::macros::{find_assert_eq_args, first_node_macro_backtrace};
use clippy_utils::{ast_utils::is_useless_with_eq_exprs, eq_expr_value, is_in_test_function}; use clippy_utils::{ast_utils::is_useless_with_eq_exprs, eq_expr_value, is_in_test_function};
use rustc_hir::{BinOpKind, Expr}; use rustc_hir::{BinOpKind, Expr};
@ -35,11 +35,16 @@ pub(crate) fn check<'tcx>(
right: &'tcx Expr<'_>, right: &'tcx Expr<'_>,
) { ) {
if is_useless_with_eq_exprs(op.into()) && eq_expr_value(cx, left, right) && !is_in_test_function(cx.tcx, e.hir_id) { if is_useless_with_eq_exprs(op.into()) && eq_expr_value(cx, left, right) && !is_in_test_function(cx.tcx, e.hir_id) {
span_lint( span_lint_and_then(
cx, cx,
EQ_OP, EQ_OP,
e.span, e.span,
&format!("equal expressions as operands to `{}`", op.as_str()), &format!("equal expressions as operands to `{}`", op.as_str()),
|diag| {
if let BinOpKind::Ne = op && cx.typeck_results().expr_ty(left).is_floating_point() {
diag.note("if you intended to check if the operand is NaN, use `.is_nan()` instead");
}
},
); );
} }
} }

View File

@ -10,6 +10,8 @@ fn main() {
let _ = false != false; let _ = false != false;
let _ = 1.5 < 1.5; let _ = 1.5 < 1.5;
let _ = 1u64 >= 1u64; let _ = 1u64 >= 1u64;
let x = f32::NAN;
let _ = x != x;
// casts, methods, parentheses // casts, methods, parentheses
let _ = (1u32 as u64) & (1u32 as u64); let _ = (1u32 as u64) & (1u32 as u64);

View File

@ -30,143 +30,151 @@ error: equal expressions as operands to `>=`
LL | let _ = 1u64 >= 1u64; LL | let _ = 1u64 >= 1u64;
| ^^^^^^^^^^^^ | ^^^^^^^^^^^^
error: equal expressions as operands to `!=`
--> $DIR/eq_op.rs:14:13
|
LL | let _ = x != x;
| ^^^^^^
|
= note: if you intended to check if the operand is NaN, use `.is_nan()` instead
error: equal expressions as operands to `&` error: equal expressions as operands to `&`
--> $DIR/eq_op.rs:15:13 --> $DIR/eq_op.rs:17:13
| |
LL | let _ = (1u32 as u64) & (1u32 as u64); LL | let _ = (1u32 as u64) & (1u32 as u64);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: equal expressions as operands to `^` error: equal expressions as operands to `^`
--> $DIR/eq_op.rs:18:17 --> $DIR/eq_op.rs:20:17
| |
LL | let _ = 1 ^ ((((((1)))))); LL | let _ = 1 ^ ((((((1))))));
| ^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^
error: equal expressions as operands to `<` error: equal expressions as operands to `<`
--> $DIR/eq_op.rs:22:13 --> $DIR/eq_op.rs:24:13
| |
LL | let _ = (-(2) < -(2)); LL | let _ = (-(2) < -(2));
| ^^^^^^^^^^^^^ | ^^^^^^^^^^^^^
error: equal expressions as operands to `==` error: equal expressions as operands to `==`
--> $DIR/eq_op.rs:23:13 --> $DIR/eq_op.rs:25:13
| |
LL | let _ = ((1 + 1) & (1 + 1) == (1 + 1) & (1 + 1)); LL | let _ = ((1 + 1) & (1 + 1) == (1 + 1) & (1 + 1));
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: equal expressions as operands to `&` error: equal expressions as operands to `&`
--> $DIR/eq_op.rs:23:14 --> $DIR/eq_op.rs:25:14
| |
LL | let _ = ((1 + 1) & (1 + 1) == (1 + 1) & (1 + 1)); LL | let _ = ((1 + 1) & (1 + 1) == (1 + 1) & (1 + 1));
| ^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^
error: equal expressions as operands to `&` error: equal expressions as operands to `&`
--> $DIR/eq_op.rs:23:35 --> $DIR/eq_op.rs:25:35
| |
LL | let _ = ((1 + 1) & (1 + 1) == (1 + 1) & (1 + 1)); LL | let _ = ((1 + 1) & (1 + 1) == (1 + 1) & (1 + 1));
| ^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^
error: equal expressions as operands to `==` error: equal expressions as operands to `==`
--> $DIR/eq_op.rs:24:13 --> $DIR/eq_op.rs:26:13
| |
LL | let _ = (1 * 2) + (3 * 4) == 1 * 2 + 3 * 4; LL | let _ = (1 * 2) + (3 * 4) == 1 * 2 + 3 * 4;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: equal expressions as operands to `!=` error: equal expressions as operands to `!=`
--> $DIR/eq_op.rs:27:13 --> $DIR/eq_op.rs:29:13
| |
LL | let _ = ([1] != [1]); LL | let _ = ([1] != [1]);
| ^^^^^^^^^^^^ | ^^^^^^^^^^^^
error: equal expressions as operands to `!=` error: equal expressions as operands to `!=`
--> $DIR/eq_op.rs:28:13 --> $DIR/eq_op.rs:30:13
| |
LL | let _ = ((1, 2) != (1, 2)); LL | let _ = ((1, 2) != (1, 2));
| ^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^
error: equal expressions as operands to `==` error: equal expressions as operands to `==`
--> $DIR/eq_op.rs:32:13 --> $DIR/eq_op.rs:34:13
| |
LL | let _ = 1 + 1 == 2; LL | let _ = 1 + 1 == 2;
| ^^^^^^^^^^ | ^^^^^^^^^^
error: equal expressions as operands to `==` error: equal expressions as operands to `==`
--> $DIR/eq_op.rs:33:13 --> $DIR/eq_op.rs:35:13
| |
LL | let _ = 1 - 1 == 0; LL | let _ = 1 - 1 == 0;
| ^^^^^^^^^^ | ^^^^^^^^^^
error: equal expressions as operands to `-` error: equal expressions as operands to `-`
--> $DIR/eq_op.rs:33:13 --> $DIR/eq_op.rs:35:13
| |
LL | let _ = 1 - 1 == 0; LL | let _ = 1 - 1 == 0;
| ^^^^^ | ^^^^^
error: equal expressions as operands to `-` error: equal expressions as operands to `-`
--> $DIR/eq_op.rs:35:13 --> $DIR/eq_op.rs:37:13
| |
LL | let _ = 1 - 1; LL | let _ = 1 - 1;
| ^^^^^ | ^^^^^
error: equal expressions as operands to `/` error: equal expressions as operands to `/`
--> $DIR/eq_op.rs:36:13 --> $DIR/eq_op.rs:38:13
| |
LL | let _ = 1 / 1; LL | let _ = 1 / 1;
| ^^^^^ | ^^^^^
error: equal expressions as operands to `&&` error: equal expressions as operands to `&&`
--> $DIR/eq_op.rs:37:13 --> $DIR/eq_op.rs:39:13
| |
LL | let _ = true && true; LL | let _ = true && true;
| ^^^^^^^^^^^^ | ^^^^^^^^^^^^
error: equal expressions as operands to `||` error: equal expressions as operands to `||`
--> $DIR/eq_op.rs:39:13 --> $DIR/eq_op.rs:41:13
| |
LL | let _ = true || true; LL | let _ = true || true;
| ^^^^^^^^^^^^ | ^^^^^^^^^^^^
error: equal expressions as operands to `&&` error: equal expressions as operands to `&&`
--> $DIR/eq_op.rs:44:13 --> $DIR/eq_op.rs:46:13
| |
LL | let _ = a == b && b == a; LL | let _ = a == b && b == a;
| ^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^
error: equal expressions as operands to `&&` error: equal expressions as operands to `&&`
--> $DIR/eq_op.rs:45:13 --> $DIR/eq_op.rs:47:13
| |
LL | let _ = a != b && b != a; LL | let _ = a != b && b != a;
| ^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^
error: equal expressions as operands to `&&` error: equal expressions as operands to `&&`
--> $DIR/eq_op.rs:46:13 --> $DIR/eq_op.rs:48:13
| |
LL | let _ = a < b && b > a; LL | let _ = a < b && b > a;
| ^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^
error: equal expressions as operands to `&&` error: equal expressions as operands to `&&`
--> $DIR/eq_op.rs:47:13 --> $DIR/eq_op.rs:49:13
| |
LL | let _ = a <= b && b >= a; LL | let _ = a <= b && b >= a;
| ^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^
error: equal expressions as operands to `==` error: equal expressions as operands to `==`
--> $DIR/eq_op.rs:50:13 --> $DIR/eq_op.rs:52:13
| |
LL | let _ = a == a; LL | let _ = a == a;
| ^^^^^^ | ^^^^^^
error: equal expressions as operands to `/` error: equal expressions as operands to `/`
--> $DIR/eq_op.rs:60:20 --> $DIR/eq_op.rs:62:20
| |
LL | const D: u32 = A / A; LL | const D: u32 = A / A;
| ^^^^^ | ^^^^^
error: equal expressions as operands to `==` error: equal expressions as operands to `==`
--> $DIR/eq_op.rs:91:5 --> $DIR/eq_op.rs:93:5
| |
LL | (n1.inner.0).0 == (n1.inner.0).0 && (n1.inner.1).0 == (n2.inner.1).0 && (n1.inner.2).0 == (n2.inner.2).0 LL | (n1.inner.0).0 == (n1.inner.0).0 && (n1.inner.1).0 == (n2.inner.1).0 && (n1.inner.2).0 == (n2.inner.2).0
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 28 previous errors error: aborting due to 29 previous errors