@@ -18,6 +18,7 @@ mod fn_to_numeric_cast_any;
1818mod fn_to_numeric_cast_with_truncation;
1919mod ptr_as_ptr;
2020mod ptr_cast_constness;
21+ mod ptr_to_temporary;
2122mod unnecessary_cast;
2223mod utils;
2324
@@ -657,6 +658,28 @@ declare_clippy_lint! {
657658 "casting a known floating-point NaN into an integer"
658659}
659660
661+ declare_clippy_lint ! {
662+ /// ### What it does
663+ /// Checks for raw pointers that point to temporary values.
664+ ///
665+ /// ### Why is this bad?
666+ /// It will result in Undefined Behavior, as the pointer will stop pointing to valid stack
667+ /// memory once the temporary is dropped.
668+ ///
669+ /// ### Example
670+ /// ```rust,ignore
671+ /// let _: (0.0_f32 / 0.0) as u64;
672+ /// ```
673+ /// Use instead:
674+ /// ```rust,ignore
675+ /// let _: = 0_u64;
676+ /// ```
677+ #[ clippy:: version = "1.72.0" ]
678+ pub PTR_TO_TEMPORARY ,
679+ correctness,
680+ "casting a known floating-point NaN into an integer"
681+ }
682+
660683pub struct Casts {
661684 msrv : Msrv ,
662685}
@@ -691,6 +714,7 @@ impl_lint_pass!(Casts => [
691714 CAST_SLICE_FROM_RAW_PARTS ,
692715 AS_PTR_CAST_MUT ,
693716 CAST_NAN_TO_INT ,
717+ PTR_TO_TEMPORARY ,
694718] ) ;
695719
696720impl < ' tcx > LateLintPass < ' tcx > for Casts {
@@ -736,6 +760,7 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
736760 }
737761
738762 as_underscore:: check ( cx, expr, cast_to_hir) ;
763+ ptr_to_temporary:: check ( cx, expr, cast_expr, cast_to_hir) ;
739764
740765 if self . msrv . meets ( msrvs:: BORROW_AS_PTR ) {
741766 borrow_as_ptr:: check ( cx, expr, cast_expr, cast_to_hir) ;
0 commit comments