Skip to content

Commit ad419b8

Browse files
authored
check f16 and f128 in float_equality_without_abs (#15054)
followup of #141874 cc #116909 changelog: check f16 and f128 in float_equality_without_abs
2 parents b6343a5 + 6a4ab68 commit ad419b8

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

clippy_lints/src/operators/float_equality_without_abs.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ pub(crate) fn check<'tcx>(
3434
val_r,
3535
) = lhs.kind
3636

37-
// right hand side matches either f32::EPSILON or f64::EPSILON
37+
// right hand side matches _::EPSILON
3838
&& let ExprKind::Path(ref epsilon_path) = rhs.kind
3939
&& let Res::Def(DefKind::AssocConst, def_id) = cx.qpath_res(epsilon_path, rhs.hir_id)
40-
&& let Some(epsilon) = cx.tcx.get_diagnostic_name(def_id)
41-
&& matches!(epsilon, sym::f32_epsilon| sym::f64_epsilon)
40+
&& let Some(sym) = cx.tcx.get_diagnostic_name(def_id)
41+
&& matches!(sym, sym::f16_epsilon | sym::f32_epsilon | sym::f64_epsilon | sym::f128_epsilon)
4242

4343
// values of the subtractions on the left hand side are of the type float
4444
&& let t_val_l = cx.typeck_results().expr_ty(val_l)

tests/ui/float_equality_without_abs.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
#![feature(f128)]
2+
#![feature(f16)]
13
#![warn(clippy::float_equality_without_abs)]
24
//@no-rustfix: suggestions cause type ambiguity
35

4-
// FIXME(f16_f128): add tests for these types when abs is available
5-
66
pub fn is_roughly_equal(a: f32, b: f32) -> bool {
77
(a - b) < f32::EPSILON
88
//~^ float_equality_without_abs
@@ -44,10 +44,20 @@ pub fn main() {
4444
let _ = f32::EPSILON > 1.0 - 2.0;
4545
//~^ float_equality_without_abs
4646

47+
let _ = (a as f16 - b as f16) < f16::EPSILON;
48+
//~^ float_equality_without_abs
49+
50+
let _ = (a as f128 - b as f128) < f128::EPSILON;
51+
//~^ float_equality_without_abs
52+
4753
// those are correct
54+
let _ = (a as f16 - b as f16).abs() < f16::EPSILON;
4855
let _ = (a - b).abs() < f32::EPSILON;
4956
let _ = (a as f64 - b as f64).abs() < f64::EPSILON;
57+
let _ = (a as f128 - b as f128).abs() < f128::EPSILON;
5058

59+
let _ = f16::EPSILON > (a as f16 - b as f16).abs();
5160
let _ = f32::EPSILON > (a - b).abs();
5261
let _ = f64::EPSILON > (a as f64 - b as f64).abs();
62+
let _ = f128::EPSILON > (a as f128 - b as f128).abs();
5363
}

tests/ui/float_equality_without_abs.stderr

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,5 +89,21 @@ LL | let _ = f32::EPSILON > 1.0 - 2.0;
8989
| |
9090
| help: add `.abs()`: `(1.0 - 2.0).abs()`
9191

92-
error: aborting due to 11 previous errors
92+
error: float equality check without `.abs()`
93+
--> tests/ui/float_equality_without_abs.rs:47:13
94+
|
95+
LL | let _ = (a as f16 - b as f16) < f16::EPSILON;
96+
| ---------------------^^^^^^^^^^^^^^^
97+
| |
98+
| help: add `.abs()`: `(a as f16 - b as f16).abs()`
99+
100+
error: float equality check without `.abs()`
101+
--> tests/ui/float_equality_without_abs.rs:50:13
102+
|
103+
LL | let _ = (a as f128 - b as f128) < f128::EPSILON;
104+
| -----------------------^^^^^^^^^^^^^^^^
105+
| |
106+
| help: add `.abs()`: `(a as f128 - b as f128).abs()`
107+
108+
error: aborting due to 13 previous errors
93109

0 commit comments

Comments
 (0)