Skip to content

Commit

Permalink
float_cmp: Allow literal to constant comparisons.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jarcho committed Jul 19, 2024
1 parent 5bb41b2 commit 847e7d6
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 127 deletions.
11 changes: 8 additions & 3 deletions clippy_lints/src/operators/float_cmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ pub(crate) fn check<'tcx>(
&& is_float(cx, left_reduced)
&& is_float(cx, right_reduced)
// Don't lint literal comparisons
&& !(matches!(left_reduced.kind, ExprKind::Lit(_)) && matches!(right_reduced.kind, ExprKind::Lit(_)))
&& let is_left_lit = matches!(left_reduced.kind, ExprKind::Lit(_))
&& let is_right_lit = matches!(right_reduced.kind, ExprKind::Lit(_))
&& !(is_left_lit && is_right_lit)
// Allow comparing the results of signum()
&& !(is_signum(cx, left_reduced) && is_signum(cx, right_reduced))
&& match (path_res(cx, left_reduced), path_res(cx, right_reduced)) {
Expand All @@ -57,8 +59,11 @@ pub(crate) fn check<'tcx>(
return;
}

if get_named_const_def_id(cx, left_reduced).is_some_and(|id| config.allowed_constants.contains(&id))
|| get_named_const_def_id(cx, right_reduced).is_some_and(|id| config.allowed_constants.contains(&id))
// Neither `CONST == lit` or `ALLOWED_CONST == x` should lint.
if get_named_const_def_id(cx, left_reduced)
.is_some_and(|id| is_right_lit || config.allowed_constants.contains(&id))
|| get_named_const_def_id(cx, right_reduced)
.is_some_and(|id| is_left_lit || config.allowed_constants.contains(&id))
{
return;
}
Expand Down
96 changes: 48 additions & 48 deletions tests/ui-toml/float_cmp/test.change_detect.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: strict comparison of `f32` or `f64`
--> tests/ui-toml/float_cmp/test.rs:17:21
--> tests/ui-toml/float_cmp/test.rs:22:21
|
LL | let _ = x == y;
| ^^^^^^ help: consider comparing them within some margin of error: `(x - y).abs() < error_margin`
Expand All @@ -11,283 +11,283 @@ LL | #![deny(clippy::float_cmp)]
| ^^^^^^^^^^^^^^^^^

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

0 comments on commit 847e7d6

Please sign in to comment.