@@ -2605,9 +2605,30 @@ pub enum Rvalue<'tcx> {
26052605static_assert_size ! ( Rvalue <' _>, 40 ) ;
26062606
26072607impl < ' tcx > Rvalue < ' tcx > {
2608+ /// Returns true if rvalue can be safely removed when the result is unused.
26082609 #[ inline]
2609- pub fn is_pointer_int_cast ( & self ) -> bool {
2610- matches ! ( self , Rvalue :: Cast ( CastKind :: PointerExposeAddress , _, _) )
2610+ pub fn is_safe_to_remove ( & self ) -> bool {
2611+ match self {
2612+ // Pointer to int casts may be side-effects due to exposing the provenance.
2613+ // While the model is undecided, we should be conservative. See
2614+ // <https://www.ralfj.de/blog/2022/04/11/provenance-exposed.html>
2615+ Rvalue :: Cast ( CastKind :: PointerExposeAddress , _, _) => false ,
2616+
2617+ Rvalue :: Use ( _)
2618+ | Rvalue :: Repeat ( _, _)
2619+ | Rvalue :: Ref ( _, _, _)
2620+ | Rvalue :: ThreadLocalRef ( _)
2621+ | Rvalue :: AddressOf ( _, _)
2622+ | Rvalue :: Len ( _)
2623+ | Rvalue :: Cast ( CastKind :: Misc | CastKind :: Pointer ( _) , _, _)
2624+ | Rvalue :: BinaryOp ( _, _)
2625+ | Rvalue :: CheckedBinaryOp ( _, _)
2626+ | Rvalue :: NullaryOp ( _, _)
2627+ | Rvalue :: UnaryOp ( _, _)
2628+ | Rvalue :: Discriminant ( _)
2629+ | Rvalue :: Aggregate ( _, _)
2630+ | Rvalue :: ShallowInitBox ( _, _) => true ,
2631+ }
26112632 }
26122633}
26132634
0 commit comments