Skip to content

Commit efac838

Browse files
committed
Make eq_op suggest .is_nan()
1 parent 2c40b99 commit efac838

File tree

3 files changed

+41
-26
lines changed

3 files changed

+41
-26
lines changed

clippy_lints/src/operators/eq_op.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::diagnostics::span_lint;
1+
use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
22
use clippy_utils::macros::{find_assert_eq_args, first_node_macro_backtrace};
33
use clippy_utils::{ast_utils::is_useless_with_eq_exprs, eq_expr_value, is_in_test_function};
44
use rustc_hir::{BinOpKind, Expr};
@@ -35,11 +35,16 @@ pub(crate) fn check<'tcx>(
3535
right: &'tcx Expr<'_>,
3636
) {
3737
if is_useless_with_eq_exprs(op.into()) && eq_expr_value(cx, left, right) && !is_in_test_function(cx.tcx, e.hir_id) {
38-
span_lint(
38+
span_lint_and_then(
3939
cx,
4040
EQ_OP,
4141
e.span,
4242
&format!("equal expressions as operands to `{}`", op.as_str()),
43+
|diag| {
44+
if let BinOpKind::Ne = op && cx.typeck_results().expr_ty(left).is_floating_point() {
45+
diag.note("if you intended to check if the operand is NaN, use `.is_nan()` instead");
46+
}
47+
},
4348
);
4449
}
4550
}

tests/ui/eq_op.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ fn main() {
1010
let _ = false != false;
1111
let _ = 1.5 < 1.5;
1212
let _ = 1u64 >= 1u64;
13+
let x = f32::NAN;
14+
let _ = x != x;
1315

1416
// casts, methods, parentheses
1517
let _ = (1u32 as u64) & (1u32 as u64);

tests/ui/eq_op.stderr

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,143 +30,151 @@ error: equal expressions as operands to `>=`
3030
LL | let _ = 1u64 >= 1u64;
3131
| ^^^^^^^^^^^^
3232

33+
error: equal expressions as operands to `!=`
34+
--> $DIR/eq_op.rs:14:13
35+
|
36+
LL | let _ = x != x;
37+
| ^^^^^^
38+
|
39+
= note: if you intended to check if the operand is NaN, use `.is_nan()` instead
40+
3341
error: equal expressions as operands to `&`
34-
--> $DIR/eq_op.rs:15:13
42+
--> $DIR/eq_op.rs:17:13
3543
|
3644
LL | let _ = (1u32 as u64) & (1u32 as u64);
3745
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3846

3947
error: equal expressions as operands to `^`
40-
--> $DIR/eq_op.rs:18:17
48+
--> $DIR/eq_op.rs:20:17
4149
|
4250
LL | let _ = 1 ^ ((((((1))))));
4351
| ^^^^^^^^^^^^^^^^^
4452

4553
error: equal expressions as operands to `<`
46-
--> $DIR/eq_op.rs:22:13
54+
--> $DIR/eq_op.rs:24:13
4755
|
4856
LL | let _ = (-(2) < -(2));
4957
| ^^^^^^^^^^^^^
5058

5159
error: equal expressions as operands to `==`
52-
--> $DIR/eq_op.rs:23:13
60+
--> $DIR/eq_op.rs:25:13
5361
|
5462
LL | let _ = ((1 + 1) & (1 + 1) == (1 + 1) & (1 + 1));
5563
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5664

5765
error: equal expressions as operands to `&`
58-
--> $DIR/eq_op.rs:23:14
66+
--> $DIR/eq_op.rs:25:14
5967
|
6068
LL | let _ = ((1 + 1) & (1 + 1) == (1 + 1) & (1 + 1));
6169
| ^^^^^^^^^^^^^^^^^
6270

6371
error: equal expressions as operands to `&`
64-
--> $DIR/eq_op.rs:23:35
72+
--> $DIR/eq_op.rs:25:35
6573
|
6674
LL | let _ = ((1 + 1) & (1 + 1) == (1 + 1) & (1 + 1));
6775
| ^^^^^^^^^^^^^^^^^
6876

6977
error: equal expressions as operands to `==`
70-
--> $DIR/eq_op.rs:24:13
78+
--> $DIR/eq_op.rs:26:13
7179
|
7280
LL | let _ = (1 * 2) + (3 * 4) == 1 * 2 + 3 * 4;
7381
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7482

7583
error: equal expressions as operands to `!=`
76-
--> $DIR/eq_op.rs:27:13
84+
--> $DIR/eq_op.rs:29:13
7785
|
7886
LL | let _ = ([1] != [1]);
7987
| ^^^^^^^^^^^^
8088

8189
error: equal expressions as operands to `!=`
82-
--> $DIR/eq_op.rs:28:13
90+
--> $DIR/eq_op.rs:30:13
8391
|
8492
LL | let _ = ((1, 2) != (1, 2));
8593
| ^^^^^^^^^^^^^^^^^^
8694

8795
error: equal expressions as operands to `==`
88-
--> $DIR/eq_op.rs:32:13
96+
--> $DIR/eq_op.rs:34:13
8997
|
9098
LL | let _ = 1 + 1 == 2;
9199
| ^^^^^^^^^^
92100

93101
error: equal expressions as operands to `==`
94-
--> $DIR/eq_op.rs:33:13
102+
--> $DIR/eq_op.rs:35:13
95103
|
96104
LL | let _ = 1 - 1 == 0;
97105
| ^^^^^^^^^^
98106

99107
error: equal expressions as operands to `-`
100-
--> $DIR/eq_op.rs:33:13
108+
--> $DIR/eq_op.rs:35:13
101109
|
102110
LL | let _ = 1 - 1 == 0;
103111
| ^^^^^
104112

105113
error: equal expressions as operands to `-`
106-
--> $DIR/eq_op.rs:35:13
114+
--> $DIR/eq_op.rs:37:13
107115
|
108116
LL | let _ = 1 - 1;
109117
| ^^^^^
110118

111119
error: equal expressions as operands to `/`
112-
--> $DIR/eq_op.rs:36:13
120+
--> $DIR/eq_op.rs:38:13
113121
|
114122
LL | let _ = 1 / 1;
115123
| ^^^^^
116124

117125
error: equal expressions as operands to `&&`
118-
--> $DIR/eq_op.rs:37:13
126+
--> $DIR/eq_op.rs:39:13
119127
|
120128
LL | let _ = true && true;
121129
| ^^^^^^^^^^^^
122130

123131
error: equal expressions as operands to `||`
124-
--> $DIR/eq_op.rs:39:13
132+
--> $DIR/eq_op.rs:41:13
125133
|
126134
LL | let _ = true || true;
127135
| ^^^^^^^^^^^^
128136

129137
error: equal expressions as operands to `&&`
130-
--> $DIR/eq_op.rs:44:13
138+
--> $DIR/eq_op.rs:46:13
131139
|
132140
LL | let _ = a == b && b == a;
133141
| ^^^^^^^^^^^^^^^^
134142

135143
error: equal expressions as operands to `&&`
136-
--> $DIR/eq_op.rs:45:13
144+
--> $DIR/eq_op.rs:47:13
137145
|
138146
LL | let _ = a != b && b != a;
139147
| ^^^^^^^^^^^^^^^^
140148

141149
error: equal expressions as operands to `&&`
142-
--> $DIR/eq_op.rs:46:13
150+
--> $DIR/eq_op.rs:48:13
143151
|
144152
LL | let _ = a < b && b > a;
145153
| ^^^^^^^^^^^^^^
146154

147155
error: equal expressions as operands to `&&`
148-
--> $DIR/eq_op.rs:47:13
156+
--> $DIR/eq_op.rs:49:13
149157
|
150158
LL | let _ = a <= b && b >= a;
151159
| ^^^^^^^^^^^^^^^^
152160

153161
error: equal expressions as operands to `==`
154-
--> $DIR/eq_op.rs:50:13
162+
--> $DIR/eq_op.rs:52:13
155163
|
156164
LL | let _ = a == a;
157165
| ^^^^^^
158166

159167
error: equal expressions as operands to `/`
160-
--> $DIR/eq_op.rs:60:20
168+
--> $DIR/eq_op.rs:62:20
161169
|
162170
LL | const D: u32 = A / A;
163171
| ^^^^^
164172

165173
error: equal expressions as operands to `==`
166-
--> $DIR/eq_op.rs:91:5
174+
--> $DIR/eq_op.rs:93:5
167175
|
168176
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
169177
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
170178

171-
error: aborting due to 28 previous errors
179+
error: aborting due to 29 previous errors
172180

0 commit comments

Comments
 (0)