@@ -4,6 +4,7 @@ mod borrow_as_ptr;
44mod cast_abs_to_unsigned;
55mod cast_enum_constructor;
66mod cast_lossless;
7+ mod cast_nan_to_int;
78mod cast_possible_truncation;
89mod cast_possible_wrap;
910mod cast_precision_loss;
@@ -570,6 +571,7 @@ declare_clippy_lint! {
570571 pedantic,
571572 "borrowing just to cast to a raw pointer"
572573}
574+
573575declare_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+
626650pub 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
661686impl < ' 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) ;
0 commit comments