Skip to content

Commit 847e7d6

Browse files
committed
float_cmp: Allow literal to constant comparisons.
1 parent 5bb41b2 commit 847e7d6

File tree

5 files changed

+145
-127
lines changed

5 files changed

+145
-127
lines changed

clippy_lints/src/operators/float_cmp.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ pub(crate) fn check<'tcx>(
3131
&& is_float(cx, left_reduced)
3232
&& is_float(cx, right_reduced)
3333
// Don't lint literal comparisons
34-
&& !(matches!(left_reduced.kind, ExprKind::Lit(_)) && matches!(right_reduced.kind, ExprKind::Lit(_)))
34+
&& let is_left_lit = matches!(left_reduced.kind, ExprKind::Lit(_))
35+
&& let is_right_lit = matches!(right_reduced.kind, ExprKind::Lit(_))
36+
&& !(is_left_lit && is_right_lit)
3537
// Allow comparing the results of signum()
3638
&& !(is_signum(cx, left_reduced) && is_signum(cx, right_reduced))
3739
&& match (path_res(cx, left_reduced), path_res(cx, right_reduced)) {
@@ -57,8 +59,11 @@ pub(crate) fn check<'tcx>(
5759
return;
5860
}
5961

60-
if get_named_const_def_id(cx, left_reduced).is_some_and(|id| config.allowed_constants.contains(&id))
61-
|| get_named_const_def_id(cx, right_reduced).is_some_and(|id| config.allowed_constants.contains(&id))
62+
// Neither `CONST == lit` or `ALLOWED_CONST == x` should lint.
63+
if get_named_const_def_id(cx, left_reduced)
64+
.is_some_and(|id| is_right_lit || config.allowed_constants.contains(&id))
65+
|| get_named_const_def_id(cx, right_reduced)
66+
.is_some_and(|id| is_left_lit || config.allowed_constants.contains(&id))
6267
{
6368
return;
6469
}

tests/ui-toml/float_cmp/test.change_detect.stderr

+48-48
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: strict comparison of `f32` or `f64`
2-
--> tests/ui-toml/float_cmp/test.rs:17:21
2+
--> tests/ui-toml/float_cmp/test.rs:22:21
33
|
44
LL | let _ = x == y;
55
| ^^^^^^ help: consider comparing them within some margin of error: `(x - y).abs() < error_margin`
@@ -11,283 +11,283 @@ LL | #![deny(clippy::float_cmp)]
1111
| ^^^^^^^^^^^^^^^^^
1212

1313
error: strict comparison of `f32` or `f64`
14-
--> tests/ui-toml/float_cmp/test.rs:18:21
14+
--> tests/ui-toml/float_cmp/test.rs:23:21
1515
|
1616
LL | let _ = x != y;
1717
| ^^^^^^ help: consider comparing them within some margin of error: `(x - y).abs() > error_margin`
1818

1919
error: strict comparison of `f32` or `f64`
20-
--> tests/ui-toml/float_cmp/test.rs:19:21
20+
--> tests/ui-toml/float_cmp/test.rs:24:21
2121
|
2222
LL | let _ = x == 5.5;
2323
| ^^^^^^^^ help: consider comparing them within some margin of error: `(x - 5.5).abs() < error_margin`
2424

2525
error: strict comparison of `f32` or `f64`
26-
--> tests/ui-toml/float_cmp/test.rs:20:21
26+
--> tests/ui-toml/float_cmp/test.rs:25:21
2727
|
2828
LL | let _ = 5.5 == x;
2929
| ^^^^^^^^ help: consider comparing them within some margin of error: `(5.5 - x).abs() < error_margin`
3030

3131
error: strict comparison of `f32` or `f64`
32-
--> tests/ui-toml/float_cmp/test.rs:43:21
32+
--> tests/ui-toml/float_cmp/test.rs:48:21
3333
|
3434
LL | let _ = x == y;
3535
| ^^^^^^ help: consider comparing them within some margin of error: `(x - y).abs() < error_margin`
3636

3737
error: strict comparison of `f32` or `f64`
38-
--> tests/ui-toml/float_cmp/test.rs:44:21
38+
--> tests/ui-toml/float_cmp/test.rs:49:21
3939
|
4040
LL | let _ = x != y;
4141
| ^^^^^^ help: consider comparing them within some margin of error: `(x - y).abs() > error_margin`
4242

4343
error: strict comparison of `f32` or `f64`
44-
--> tests/ui-toml/float_cmp/test.rs:45:21
44+
--> tests/ui-toml/float_cmp/test.rs:50:21
4545
|
4646
LL | let _ = x == 5.5;
4747
| ^^^^^^^^ help: consider comparing them within some margin of error: `(x - 5.5).abs() < error_margin`
4848

4949
error: strict comparison of `f32` or `f64`
50-
--> tests/ui-toml/float_cmp/test.rs:46:21
50+
--> tests/ui-toml/float_cmp/test.rs:51:21
5151
|
5252
LL | let _ = 5.5 == x;
5353
| ^^^^^^^^ help: consider comparing them within some margin of error: `(5.5 - x).abs() < error_margin`
5454

5555
error: strict comparison of `f32` or `f64` arrays
56-
--> tests/ui-toml/float_cmp/test.rs:69:21
56+
--> tests/ui-toml/float_cmp/test.rs:74:21
5757
|
5858
LL | let _ = x == y;
5959
| ^^^^^^
6060

6161
error: strict comparison of `f32` or `f64` arrays
62-
--> tests/ui-toml/float_cmp/test.rs:70:21
62+
--> tests/ui-toml/float_cmp/test.rs:75:21
6363
|
6464
LL | let _ = x == [5.5; 4];
6565
| ^^^^^^^^^^^^^
6666

6767
error: strict comparison of `f32` or `f64` arrays
68-
--> tests/ui-toml/float_cmp/test.rs:71:21
68+
--> tests/ui-toml/float_cmp/test.rs:76:21
6969
|
7070
LL | let _ = [5.5; 4] == x;
7171
| ^^^^^^^^^^^^^
7272

7373
error: strict comparison of `f32` or `f64` arrays
74-
--> tests/ui-toml/float_cmp/test.rs:72:21
74+
--> tests/ui-toml/float_cmp/test.rs:77:21
7575
|
7676
LL | let _ = [0.0, 0.0, 0.0, 5.5] == x;
7777
| ^^^^^^^^^^^^^^^^^^^^^^^^^
7878

7979
error: strict comparison of `f32` or `f64` arrays
80-
--> tests/ui-toml/float_cmp/test.rs:73:21
80+
--> tests/ui-toml/float_cmp/test.rs:78:21
8181
|
8282
LL | let _ = x == [0.0, 0.0, 0.0, 5.5];
8383
| ^^^^^^^^^^^^^^^^^^^^^^^^^
8484

8585
error: strict comparison of `f32` or `f64` arrays
86-
--> tests/ui-toml/float_cmp/test.rs:89:21
86+
--> tests/ui-toml/float_cmp/test.rs:94:21
8787
|
8888
LL | let _ = x == y;
8989
| ^^^^^^
9090

9191
error: strict comparison of `f32` or `f64` arrays
92-
--> tests/ui-toml/float_cmp/test.rs:90:21
92+
--> tests/ui-toml/float_cmp/test.rs:95:21
9393
|
9494
LL | let _ = x == [5.5; 4];
9595
| ^^^^^^^^^^^^^
9696

9797
error: strict comparison of `f32` or `f64` arrays
98-
--> tests/ui-toml/float_cmp/test.rs:91:21
98+
--> tests/ui-toml/float_cmp/test.rs:96:21
9999
|
100100
LL | let _ = [5.5; 4] == x;
101101
| ^^^^^^^^^^^^^
102102

103103
error: strict comparison of `f32` or `f64` arrays
104-
--> tests/ui-toml/float_cmp/test.rs:92:21
104+
--> tests/ui-toml/float_cmp/test.rs:97:21
105105
|
106106
LL | let _ = [0.0, 0.0, 0.0, 5.5] == x;
107107
| ^^^^^^^^^^^^^^^^^^^^^^^^^
108108

109109
error: strict comparison of `f32` or `f64` arrays
110-
--> tests/ui-toml/float_cmp/test.rs:93:21
110+
--> tests/ui-toml/float_cmp/test.rs:98:21
111111
|
112112
LL | let _ = x == [0.0, 0.0, 0.0, 5.5];
113113
| ^^^^^^^^^^^^^^^^^^^^^^^^^
114114

115115
error: strict comparison of `f32` or `f64`
116-
--> tests/ui-toml/float_cmp/test.rs:111:21
116+
--> tests/ui-toml/float_cmp/test.rs:116:21
117117
|
118118
LL | let _ = x == y;
119119
| ^^^^^^ help: consider comparing them within some margin of error: `(x - y).abs() < error_margin`
120120

121121
error: strict comparison of `f32` or `f64` arrays
122-
--> tests/ui-toml/float_cmp/test.rs:117:21
122+
--> tests/ui-toml/float_cmp/test.rs:122:21
123123
|
124124
LL | let _ = x == y;
125125
| ^^^^^^
126126

127127
error: strict comparison of `f32` or `f64`
128-
--> tests/ui-toml/float_cmp/test.rs:132:21
128+
--> tests/ui-toml/float_cmp/test.rs:137:21
129129
|
130130
LL | let _ = f32::EPSILON * x == x * x;
131131
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(f32::EPSILON * x - x * x).abs() < error_margin`
132132

133133
error: strict comparison of `f32` or `f64`
134-
--> tests/ui-toml/float_cmp/test.rs:133:21
134+
--> tests/ui-toml/float_cmp/test.rs:138:21
135135
|
136136
LL | let _ = x * x == f32::EPSILON * x;
137137
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(x * x - f32::EPSILON * x).abs() < error_margin`
138138

139139
error: strict comparison of `f32` or `f64`
140-
--> tests/ui-toml/float_cmp/test.rs:158:17
140+
--> tests/ui-toml/float_cmp/test.rs:163:17
141141
|
142142
LL | let _ = f(1.0) == f(5.0);
143143
| ^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(f(1.0) - f(5.0)).abs() < error_margin`
144144

145145
error: strict comparison of `f32` or `f64`
146-
--> tests/ui-toml/float_cmp/test.rs:159:17
146+
--> tests/ui-toml/float_cmp/test.rs:164:17
147147
|
148148
LL | let _ = 1.0 == f(5.0);
149149
| ^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(1.0 - f(5.0)).abs() < error_margin`
150150

151151
error: strict comparison of `f32` or `f64`
152-
--> tests/ui-toml/float_cmp/test.rs:160:17
152+
--> tests/ui-toml/float_cmp/test.rs:165:17
153153
|
154154
LL | let _ = f(1.0) + 1.0 != 5.0;
155155
| ^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(f(1.0) + 1.0 - 5.0).abs() > error_margin`
156156

157157
error: strict comparison of `f32` or `f64`
158-
--> tests/ui-toml/float_cmp/test.rs:202:21
158+
--> tests/ui-toml/float_cmp/test.rs:207:21
159159
|
160160
LL | let _ = x == C[1];
161161
| ^^^^^^^^^ help: consider comparing them within some margin of error: `(x - C[1]).abs() < error_margin`
162162

163163
error: strict comparison of `f32` or `f64`
164-
--> tests/ui-toml/float_cmp/test.rs:203:21
164+
--> tests/ui-toml/float_cmp/test.rs:208:21
165165
|
166166
LL | let _ = C[1] == x;
167167
| ^^^^^^^^^ help: consider comparing them within some margin of error: `(C[1] - x).abs() < error_margin`
168168

169169
error: strict comparison of `f32` or `f64`
170-
--> tests/ui-toml/float_cmp/test.rs:268:21
170+
--> tests/ui-toml/float_cmp/test.rs:273:21
171171
|
172172
LL | let _ = x == x + 1.0;
173173
| ^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(x - (x + 1.0)).abs() < error_margin`
174174

175175
error: strict comparison of `f32` or `f64`
176-
--> tests/ui-toml/float_cmp/test.rs:269:21
176+
--> tests/ui-toml/float_cmp/test.rs:274:21
177177
|
178178
LL | let _ = x + 1.0 == x;
179179
| ^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(x + 1.0 - x).abs() < error_margin`
180180

181181
error: strict comparison of `f32` or `f64`
182-
--> tests/ui-toml/float_cmp/test.rs:270:21
182+
--> tests/ui-toml/float_cmp/test.rs:275:21
183183
|
184184
LL | let _ = -x == -x + 1.0;
185185
| ^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(-x - (-x + 1.0)).abs() < error_margin`
186186

187187
error: strict comparison of `f32` or `f64`
188-
--> tests/ui-toml/float_cmp/test.rs:271:21
188+
--> tests/ui-toml/float_cmp/test.rs:276:21
189189
|
190190
LL | let _ = -x + 1.0 == -x;
191191
| ^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(-x + 1.0 - -x).abs() < error_margin`
192192

193193
error: strict comparison of `f32` or `f64`
194-
--> tests/ui-toml/float_cmp/test.rs:272:21
194+
--> tests/ui-toml/float_cmp/test.rs:277:21
195195
|
196196
LL | let _ = x == f1(x);
197197
| ^^^^^^^^^^ help: consider comparing them within some margin of error: `(x - f1(x)).abs() < error_margin`
198198

199199
error: strict comparison of `f32` or `f64`
200-
--> tests/ui-toml/float_cmp/test.rs:273:21
200+
--> tests/ui-toml/float_cmp/test.rs:278:21
201201
|
202202
LL | let _ = f1(x) == x;
203203
| ^^^^^^^^^^ help: consider comparing them within some margin of error: `(f1(x) - x).abs() < error_margin`
204204

205205
error: strict comparison of `f32` or `f64`
206-
--> tests/ui-toml/float_cmp/test.rs:274:21
206+
--> tests/ui-toml/float_cmp/test.rs:279:21
207207
|
208208
LL | let _ = x == f2(x, y);
209209
| ^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(x - f2(x, y)).abs() < error_margin`
210210

211211
error: strict comparison of `f32` or `f64`
212-
--> tests/ui-toml/float_cmp/test.rs:275:21
212+
--> tests/ui-toml/float_cmp/test.rs:280:21
213213
|
214214
LL | let _ = f2(x, y) == x;
215215
| ^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(f2(x, y) - x).abs() < error_margin`
216216

217217
error: strict comparison of `f32` or `f64`
218-
--> tests/ui-toml/float_cmp/test.rs:276:21
218+
--> tests/ui-toml/float_cmp/test.rs:281:21
219219
|
220220
LL | let _ = f1(f1(x)) == f1(x);
221221
| ^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(f1(f1(x)) - f1(x)).abs() < error_margin`
222222

223223
error: strict comparison of `f32` or `f64`
224-
--> tests/ui-toml/float_cmp/test.rs:277:21
224+
--> tests/ui-toml/float_cmp/test.rs:282:21
225225
|
226226
LL | let _ = f1(x) == f1(f1(x));
227227
| ^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(f1(x) - f1(f1(x))).abs() < error_margin`
228228

229229
error: strict comparison of `f32` or `f64`
230-
--> tests/ui-toml/float_cmp/test.rs:280:21
230+
--> tests/ui-toml/float_cmp/test.rs:285:21
231231
|
232232
LL | let _ = z.0 == z.0 + 1.0;
233233
| ^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(z.0 - (z.0 + 1.0)).abs() < error_margin`
234234

235235
error: strict comparison of `f32` or `f64`
236-
--> tests/ui-toml/float_cmp/test.rs:281:21
236+
--> tests/ui-toml/float_cmp/test.rs:286:21
237237
|
238238
LL | let _ = z.0 + 1.0 == z.0;
239239
| ^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(z.0 + 1.0 - z.0).abs() < error_margin`
240240

241241
error: strict comparison of `f32` or `f64`
242-
--> tests/ui-toml/float_cmp/test.rs:285:21
242+
--> tests/ui-toml/float_cmp/test.rs:290:21
243243
|
244244
LL | let _ = *x + 1.0 == *x;
245245
| ^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(*x + 1.0 - *x).abs() < error_margin`
246246

247247
error: strict comparison of `f32` or `f64`
248-
--> tests/ui-toml/float_cmp/test.rs:286:21
248+
--> tests/ui-toml/float_cmp/test.rs:291:21
249249
|
250250
LL | let _ = *x == *x + 1.0;
251251
| ^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(*x - (*x + 1.0)).abs() < error_margin`
252252

253253
error: strict comparison of `f32` or `f64`
254-
--> tests/ui-toml/float_cmp/test.rs:287:21
254+
--> tests/ui-toml/float_cmp/test.rs:292:21
255255
|
256256
LL | let _ = *x == f1(*x);
257257
| ^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(*x - f1(*x)).abs() < error_margin`
258258

259259
error: strict comparison of `f32` or `f64`
260-
--> tests/ui-toml/float_cmp/test.rs:288:21
260+
--> tests/ui-toml/float_cmp/test.rs:293:21
261261
|
262262
LL | let _ = f1(*x) == *x;
263263
| ^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(f1(*x) - *x).abs() < error_margin`
264264

265265
error: strict comparison of `f32` or `f64`
266-
--> tests/ui-toml/float_cmp/test.rs:293:21
266+
--> tests/ui-toml/float_cmp/test.rs:298:21
267267
|
268268
LL | let _ = x.next().unwrap() == x.next().unwrap() + 1.0;
269269
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(x.next().unwrap() - (x.next().unwrap() + 1.0)).abs() < error_margin`
270270

271271
error: strict comparison of `f32` or `f64`
272-
--> tests/ui-toml/float_cmp/test.rs:309:21
272+
--> tests/ui-toml/float_cmp/test.rs:314:21
273273
|
274274
LL | let _ = x.f() + 1.0 == x.f();
275275
| ^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(x.f() + 1.0 - x.f()).abs() < error_margin`
276276

277277
error: strict comparison of `f32` or `f64`
278-
--> tests/ui-toml/float_cmp/test.rs:310:21
278+
--> tests/ui-toml/float_cmp/test.rs:315:21
279279
|
280280
LL | let _ = x.f() == x.f() + 1.0;
281281
| ^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(x.f() - (x.f() + 1.0)).abs() < error_margin`
282282

283283
error: strict comparison of `f32` or `f64`
284-
--> tests/ui-toml/float_cmp/test.rs:315:17
284+
--> tests/ui-toml/float_cmp/test.rs:320:17
285285
|
286286
LL | let _ = f(1.0) == f(1.0) + 1.0;
287287
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(f(1.0) - (f(1.0) + 1.0)).abs() < error_margin`
288288

289289
error: strict comparison of `f32` or `f64`
290-
--> tests/ui-toml/float_cmp/test.rs:319:17
290+
--> tests/ui-toml/float_cmp/test.rs:324:17
291291
|
292292
LL | let _ = f(1.0) == f(1.0) + 1.0;
293293
| ^^^^^^^^^^^^^^^^^^^^^^ help: consider comparing them within some margin of error: `(f(1.0) - (f(1.0) + 1.0)).abs() < error_margin`

0 commit comments

Comments
 (0)