@@ -3,6 +3,7 @@ mod borrow_as_ptr;
3
3
mod cast_abs_to_unsigned;
4
4
mod cast_enum_constructor;
5
5
mod cast_lossless;
6
+ mod cast_nan_to_int;
6
7
mod cast_possible_truncation;
7
8
mod cast_possible_wrap;
8
9
mod cast_precision_loss;
@@ -569,6 +570,7 @@ declare_clippy_lint! {
569
570
pedantic,
570
571
"borrowing just to cast to a raw pointer"
571
572
}
573
+
572
574
declare_clippy_lint ! {
573
575
/// ### What it does
574
576
/// Checks for a raw slice being cast to a slice pointer
@@ -596,6 +598,28 @@ declare_clippy_lint! {
596
598
"casting a slice created from a pointer and length to a slice pointer"
597
599
}
598
600
601
+ declare_clippy_lint ! {
602
+ /// ### What it does
603
+ /// Checks for a known NaN float being cast to an integer
604
+ ///
605
+ /// ### Why is this bad?
606
+ /// NaNs are cast into zero, so one could simply use this and make the
607
+ /// code more readable. The lint could also hint at a programmer error.
608
+ ///
609
+ /// ### Example
610
+ /// ```rust,ignore
611
+ /// let _: (0.0_f32 / 0.0) as u64;
612
+ /// ```
613
+ /// Use instead:
614
+ /// ```rust,ignore
615
+ /// let _: = 0_u64;
616
+ /// ```
617
+ #[ clippy:: version = "1.64.0" ]
618
+ pub CAST_NAN_TO_INT ,
619
+ suspicious,
620
+ "casting a known floating-point NaN into an integer"
621
+ }
622
+
599
623
pub struct Casts {
600
624
msrv : Option < RustcVersion > ,
601
625
}
@@ -627,7 +651,8 @@ impl_lint_pass!(Casts => [
627
651
CAST_ABS_TO_UNSIGNED ,
628
652
AS_UNDERSCORE ,
629
653
BORROW_AS_PTR ,
630
- CAST_SLICE_FROM_RAW_PARTS
654
+ CAST_SLICE_FROM_RAW_PARTS ,
655
+ CAST_NAN_TO_INT ,
631
656
] ) ;
632
657
633
658
impl < ' tcx > LateLintPass < ' tcx > for Casts {
@@ -664,6 +689,7 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
664
689
cast_precision_loss:: check ( cx, expr, cast_from, cast_to) ;
665
690
cast_sign_loss:: check ( cx, expr, cast_expr, cast_from, cast_to) ;
666
691
cast_abs_to_unsigned:: check ( cx, expr, cast_expr, cast_from, cast_to, self . msrv ) ;
692
+ cast_nan_to_int:: check ( cx, expr, cast_expr, cast_from, cast_to) ;
667
693
}
668
694
cast_lossless:: check ( cx, expr, cast_expr, cast_from, cast_to, self . msrv ) ;
669
695
cast_enum_constructor:: check ( cx, expr, cast_expr, cast_from) ;
0 commit comments