@@ -3,6 +3,7 @@ mod borrow_as_ptr;
33mod cast_abs_to_unsigned;
44mod cast_enum_constructor;
55mod cast_lossless;
6+ mod cast_nan_to_int;
67mod cast_possible_truncation;
78mod cast_possible_wrap;
89mod cast_precision_loss;
@@ -569,6 +570,7 @@ declare_clippy_lint! {
569570 pedantic,
570571 "borrowing just to cast to a raw pointer"
571572}
573+
572574declare_clippy_lint ! {
573575 /// ### What it does
574576 /// Checks for a raw slice being cast to a slice pointer
@@ -596,6 +598,28 @@ declare_clippy_lint! {
596598 "casting a slice created from a pointer and length to a slice pointer"
597599}
598600
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+
599623pub struct Casts {
600624 msrv : Option < RustcVersion > ,
601625}
@@ -627,7 +651,8 @@ impl_lint_pass!(Casts => [
627651 CAST_ABS_TO_UNSIGNED ,
628652 AS_UNDERSCORE ,
629653 BORROW_AS_PTR ,
630- CAST_SLICE_FROM_RAW_PARTS
654+ CAST_SLICE_FROM_RAW_PARTS ,
655+ CAST_NAN_TO_INT ,
631656] ) ;
632657
633658impl < ' tcx > LateLintPass < ' tcx > for Casts {
@@ -664,6 +689,7 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
664689 cast_precision_loss:: check ( cx, expr, cast_from, cast_to) ;
665690 cast_sign_loss:: check ( cx, expr, cast_expr, cast_from, cast_to) ;
666691 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) ;
667693 }
668694 cast_lossless:: check ( cx, expr, cast_expr, cast_from, cast_to, self . msrv ) ;
669695 cast_enum_constructor:: check ( cx, expr, cast_expr, cast_from) ;
0 commit comments