Skip to content

Commit 2880da4

Browse files
committed
Support f16 and f128 in cast_nan_to_int lint
1 parent e03c7af commit 2880da4

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

clippy_lints/src/casts/cast_nan_to_int.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>,
2121

2222
fn is_known_nan(cx: &LateContext<'_>, e: &Expr<'_>) -> bool {
2323
match ConstEvalCtxt::new(cx).eval(e) {
24-
// FIXME(f16_f128): add these types when nan checks are available on all platforms
24+
Some(Constant::F128(n)) => n.is_nan(),
2525
Some(Constant::F64(n)) => n.is_nan(),
2626
Some(Constant::F32(n)) => n.is_nan(),
27+
Some(Constant::F16(n)) => n.is_nan(),
2728
_ => false,
2829
}
2930
}

tests/ui/cast_nan_to_int.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// FIXME(f16_f128): add tests when constants are available
2-
1+
#![feature(f128)]
2+
#![feature(f16)]
33
#![warn(clippy::cast_nan_to_int)]
44
#![allow(clippy::eq_op)]
55

@@ -22,6 +22,12 @@ fn main() {
2222
let _ = (f32::INFINITY / f32::NEG_INFINITY) as usize;
2323
//~^ cast_nan_to_int
2424

25+
let _ = f16::NAN as usize;
26+
//~^ cast_nan_to_int
27+
28+
let _ = f128::NAN as u128;
29+
//~^ cast_nan_to_int
30+
2531
// those won't be linted:
2632
let _ = (1.0_f32 / 0.0) as usize;
2733
let _ = (f32::INFINITY * f32::NEG_INFINITY) as usize;

tests/ui/cast_nan_to_int.stderr

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,21 @@ LL | let _ = (f32::INFINITY / f32::NEG_INFINITY) as usize;
4848
|
4949
= note: this always evaluates to 0
5050

51-
error: aborting due to 6 previous errors
51+
error: casting a known NaN to usize
52+
--> tests/ui/cast_nan_to_int.rs:25:13
53+
|
54+
LL | let _ = f16::NAN as usize;
55+
| ^^^^^^^^^^^^^^^^^
56+
|
57+
= note: this always evaluates to 0
58+
59+
error: casting a known NaN to u128
60+
--> tests/ui/cast_nan_to_int.rs:28:13
61+
|
62+
LL | let _ = f128::NAN as u128;
63+
| ^^^^^^^^^^^^^^^^^
64+
|
65+
= note: this always evaluates to 0
66+
67+
error: aborting due to 8 previous errors
5268

0 commit comments

Comments
 (0)