@@ -4,6 +4,7 @@ mod borrow_as_ptr;
4
4
mod cast_abs_to_unsigned;
5
5
mod cast_enum_constructor;
6
6
mod cast_lossless;
7
+ mod cast_nan_to_int;
7
8
mod cast_possible_truncation;
8
9
mod cast_possible_wrap;
9
10
mod cast_precision_loss;
@@ -570,6 +571,7 @@ declare_clippy_lint! {
570
571
pedantic,
571
572
"borrowing just to cast to a raw pointer"
572
573
}
574
+
573
575
declare_clippy_lint ! {
574
576
/// ### What it does
575
577
/// Checks for a raw slice being cast to a slice pointer
@@ -623,6 +625,28 @@ declare_clippy_lint! {
623
625
"casting the result of the `&self`-taking `as_ptr` to a mutabe pointer"
624
626
}
625
627
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
+
626
650
pub struct Casts {
627
651
msrv : Option < RustcVersion > ,
628
652
}
@@ -656,6 +680,7 @@ impl_lint_pass!(Casts => [
656
680
BORROW_AS_PTR ,
657
681
CAST_SLICE_FROM_RAW_PARTS ,
658
682
AS_PTR_CAST_MUT ,
683
+ CAST_NAN_TO_INT ,
659
684
] ) ;
660
685
661
686
impl < ' tcx > LateLintPass < ' tcx > for Casts {
@@ -693,6 +718,7 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
693
718
cast_precision_loss:: check ( cx, expr, cast_from, cast_to) ;
694
719
cast_sign_loss:: check ( cx, expr, cast_expr, cast_from, cast_to) ;
695
720
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) ;
696
722
}
697
723
cast_lossless:: check ( cx, expr, cast_expr, cast_from, cast_to, self . msrv ) ;
698
724
cast_enum_constructor:: check ( cx, expr, cast_expr, cast_from) ;
0 commit comments