Skip to content

Commit b8a9a50

Browse files
committed
Auto merge of rust-lang#9617 - llogiq:cast-nan-to-int, r=Alexendoo
add `cast-nan-to-int` lint This fixes rust-lang#371. r? `@Alexendoo` --- changelog: add [`cast-nan-to-int`] lint
2 parents 6354d12 + e4c80f2 commit b8a9a50

10 files changed

+143
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3773,6 +3773,7 @@ Released 2018-09-13
37733773
[`cast_enum_constructor`]: https://rust-lang.github.io/rust-clippy/master/index.html#cast_enum_constructor
37743774
[`cast_enum_truncation`]: https://rust-lang.github.io/rust-clippy/master/index.html#cast_enum_truncation
37753775
[`cast_lossless`]: https://rust-lang.github.io/rust-clippy/master/index.html#cast_lossless
3776+
[`cast_nan_to_int`]: https://rust-lang.github.io/rust-clippy/master/index.html#cast_nan_to_int
37763777
[`cast_possible_truncation`]: https://rust-lang.github.io/rust-clippy/master/index.html#cast_possible_truncation
37773778
[`cast_possible_wrap`]: https://rust-lang.github.io/rust-clippy/master/index.html#cast_possible_wrap
37783779
[`cast_precision_loss`]: https://rust-lang.github.io/rust-clippy/master/index.html#cast_precision_loss
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use super::CAST_NAN_TO_INT;
2+
3+
use clippy_utils::consts::{constant, Constant};
4+
use clippy_utils::diagnostics::span_lint_and_note;
5+
use rustc_hir::Expr;
6+
use rustc_lint::LateContext;
7+
use rustc_middle::ty::Ty;
8+
9+
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>, from_ty: Ty<'_>, to_ty: Ty<'_>) {
10+
if from_ty.is_floating_point() && to_ty.is_integral() && is_known_nan(cx, cast_expr) {
11+
span_lint_and_note(
12+
cx,
13+
CAST_NAN_TO_INT,
14+
expr.span,
15+
&format!("casting a known NaN to {to_ty}"),
16+
None,
17+
"this always evaluates to 0",
18+
);
19+
}
20+
}
21+
22+
fn is_known_nan(cx: &LateContext<'_>, e: &Expr<'_>) -> bool {
23+
match constant(cx, cx.typeck_results(), e) {
24+
Some((Constant::F64(n), _)) => n.is_nan(),
25+
Some((Constant::F32(n), _)) => n.is_nan(),
26+
_ => false,
27+
}
28+
}

clippy_lints/src/casts/mod.rs

+26
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ mod borrow_as_ptr;
44
mod cast_abs_to_unsigned;
55
mod cast_enum_constructor;
66
mod cast_lossless;
7+
mod cast_nan_to_int;
78
mod cast_possible_truncation;
89
mod cast_possible_wrap;
910
mod cast_precision_loss;
@@ -570,6 +571,7 @@ declare_clippy_lint! {
570571
pedantic,
571572
"borrowing just to cast to a raw pointer"
572573
}
574+
573575
declare_clippy_lint! {
574576
/// ### What it does
575577
/// Checks for a raw slice being cast to a slice pointer
@@ -623,6 +625,28 @@ declare_clippy_lint! {
623625
"casting the result of the `&self`-taking `as_ptr` to a mutabe pointer"
624626
}
625627

628+
declare_clippy_lint! {
629+
/// ### What it does
630+
/// Checks for a known NaN float being cast to an integer
631+
///
632+
/// ### Why is this bad?
633+
/// NaNs are cast into zero, so one could simply use this and make the
634+
/// code more readable. The lint could also hint at a programmer error.
635+
///
636+
/// ### Example
637+
/// ```rust,ignore
638+
/// let _: (0.0_f32 / 0.0) as u64;
639+
/// ```
640+
/// Use instead:
641+
/// ```rust,ignore
642+
/// let _: = 0_u64;
643+
/// ```
644+
#[clippy::version = "1.64.0"]
645+
pub CAST_NAN_TO_INT,
646+
suspicious,
647+
"casting a known floating-point NaN into an integer"
648+
}
649+
626650
pub struct Casts {
627651
msrv: Option<RustcVersion>,
628652
}
@@ -656,6 +680,7 @@ impl_lint_pass!(Casts => [
656680
BORROW_AS_PTR,
657681
CAST_SLICE_FROM_RAW_PARTS,
658682
AS_PTR_CAST_MUT,
683+
CAST_NAN_TO_INT,
659684
]);
660685

661686
impl<'tcx> LateLintPass<'tcx> for Casts {
@@ -693,6 +718,7 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
693718
cast_precision_loss::check(cx, expr, cast_from, cast_to);
694719
cast_sign_loss::check(cx, expr, cast_expr, cast_from, cast_to);
695720
cast_abs_to_unsigned::check(cx, expr, cast_expr, cast_from, cast_to, self.msrv);
721+
cast_nan_to_int::check(cx, expr, cast_expr, cast_from, cast_to);
696722
}
697723
cast_lossless::check(cx, expr, cast_expr, cast_from, cast_to, self.msrv);
698724
cast_enum_constructor::check(cx, expr, cast_expr, cast_from);

clippy_lints/src/lib.register_all.rs

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
2525
LintId::of(casts::CAST_ABS_TO_UNSIGNED),
2626
LintId::of(casts::CAST_ENUM_CONSTRUCTOR),
2727
LintId::of(casts::CAST_ENUM_TRUNCATION),
28+
LintId::of(casts::CAST_NAN_TO_INT),
2829
LintId::of(casts::CAST_REF_TO_MUT),
2930
LintId::of(casts::CAST_SLICE_DIFFERENT_SIZES),
3031
LintId::of(casts::CAST_SLICE_FROM_RAW_PARTS),

clippy_lints/src/lib.register_lints.rs

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ store.register_lints(&[
7373
casts::CAST_ENUM_CONSTRUCTOR,
7474
casts::CAST_ENUM_TRUNCATION,
7575
casts::CAST_LOSSLESS,
76+
casts::CAST_NAN_TO_INT,
7677
casts::CAST_POSSIBLE_TRUNCATION,
7778
casts::CAST_POSSIBLE_WRAP,
7879
casts::CAST_PRECISION_LOSS,

clippy_lints/src/lib.register_suspicious.rs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ store.register_group(true, "clippy::suspicious", Some("clippy_suspicious"), vec!
1111
LintId::of(casts::CAST_ABS_TO_UNSIGNED),
1212
LintId::of(casts::CAST_ENUM_CONSTRUCTOR),
1313
LintId::of(casts::CAST_ENUM_TRUNCATION),
14+
LintId::of(casts::CAST_NAN_TO_INT),
1415
LintId::of(casts::CAST_SLICE_FROM_RAW_PARTS),
1516
LintId::of(crate_in_macro_def::CRATE_IN_MACRO_DEF),
1617
LintId::of(drop_forget_ref::DROP_NON_DROP),

src/docs.rs

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ docs! {
6161
"cast_enum_constructor",
6262
"cast_enum_truncation",
6363
"cast_lossless",
64+
"cast_nan_to_int",
6465
"cast_possible_truncation",
6566
"cast_possible_wrap",
6667
"cast_precision_loss",

src/docs/cast_nan_to_int.txt

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
### What it does
2+
Checks for a known NaN float being cast to an integer
3+
4+
### Why is this bad?
5+
NaNs are cast into zero, so one could simply use this and make the
6+
code more readable. The lint could also hint at a programmer error.
7+
8+
### Example
9+
```
10+
let _: (0.0_f32 / 0.0) as u64;
11+
```
12+
Use instead:
13+
```
14+
let _: = 0_u64;
15+
```

tests/ui/cast_nan_to_int.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#![warn(clippy::cast_nan_to_int)]
2+
#![allow(clippy::eq_op)]
3+
4+
fn main() {
5+
let _ = (0.0_f32 / -0.0) as usize;
6+
let _ = (f64::INFINITY * -0.0) as usize;
7+
let _ = (0.0 * f32::INFINITY) as usize;
8+
9+
let _ = (f64::INFINITY + f64::NEG_INFINITY) as usize;
10+
let _ = (f32::INFINITY - f32::INFINITY) as usize;
11+
let _ = (f32::INFINITY / f32::NEG_INFINITY) as usize;
12+
13+
// those won't be linted:
14+
let _ = (1.0_f32 / 0.0) as usize;
15+
let _ = (f32::INFINITY * f32::NEG_INFINITY) as usize;
16+
let _ = (f32::INFINITY - f32::NEG_INFINITY) as usize;
17+
let _ = (f64::INFINITY - 0.0) as usize;
18+
}

tests/ui/cast_nan_to_int.stderr

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
error: casting a known NaN to usize
2+
--> $DIR/cast_nan_to_int.rs:5:13
3+
|
4+
LL | let _ = (0.0_f32 / -0.0) as usize;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: this always evaluates to 0
8+
= note: `-D clippy::cast-nan-to-int` implied by `-D warnings`
9+
10+
error: casting a known NaN to usize
11+
--> $DIR/cast_nan_to_int.rs:6:13
12+
|
13+
LL | let _ = (f64::INFINITY * -0.0) as usize;
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
15+
|
16+
= note: this always evaluates to 0
17+
18+
error: casting a known NaN to usize
19+
--> $DIR/cast_nan_to_int.rs:7:13
20+
|
21+
LL | let _ = (0.0 * f32::INFINITY) as usize;
22+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
23+
|
24+
= note: this always evaluates to 0
25+
26+
error: casting a known NaN to usize
27+
--> $DIR/cast_nan_to_int.rs:9:13
28+
|
29+
LL | let _ = (f64::INFINITY + f64::NEG_INFINITY) as usize;
30+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
31+
|
32+
= note: this always evaluates to 0
33+
34+
error: casting a known NaN to usize
35+
--> $DIR/cast_nan_to_int.rs:10:13
36+
|
37+
LL | let _ = (f32::INFINITY - f32::INFINITY) as usize;
38+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
39+
|
40+
= note: this always evaluates to 0
41+
42+
error: casting a known NaN to usize
43+
--> $DIR/cast_nan_to_int.rs:11:13
44+
|
45+
LL | let _ = (f32::INFINITY / f32::NEG_INFINITY) as usize;
46+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
47+
|
48+
= note: this always evaluates to 0
49+
50+
error: aborting due to 6 previous errors
51+

0 commit comments

Comments
 (0)