diff --git a/compiler/rustc_typeck/src/check/cast.rs b/compiler/rustc_typeck/src/check/cast.rs index da054077376c3..a153997599aba 100644 --- a/compiler/rustc_typeck/src/check/cast.rs +++ b/compiler/rustc_typeck/src/check/cast.rs @@ -1041,16 +1041,12 @@ impl<'a, 'tcx> CastCheck<'tcx> { self.expr_ty, self.cast_ty )); let msg = "use `.with_addr()` to adjust a valid pointer in the same allocation, to this address"; - if let Ok(snippet) = fcx.tcx.sess.source_map().span_to_snippet(self.expr_span) { - err.span_suggestion( - self.span, - msg, - format!("(...).with_addr({snippet})"), - Applicability::HasPlaceholders, - ); - } else { - err.help(msg); - } + let suggestions = vec![ + (self.expr_span.shrink_to_lo(), String::from("(...).with_addr(")), + (self.expr_span.shrink_to_hi().to(self.cast_span), String::from(")")), + ]; + + err.multipart_suggestion(msg, suggestions, Applicability::MaybeIncorrect); err.help( "if you can't comply with strict provenance and don't have a pointer with \ the correct provenance you can use `std::ptr::from_exposed_addr()` instead" diff --git a/src/test/ui/lint/lint-strict-provenance-fuzzy-casts.stderr b/src/test/ui/lint/lint-strict-provenance-fuzzy-casts.stderr index e50d243b6ad6d..c85934aa3ba15 100644 --- a/src/test/ui/lint/lint-strict-provenance-fuzzy-casts.stderr +++ b/src/test/ui/lint/lint-strict-provenance-fuzzy-casts.stderr @@ -13,7 +13,7 @@ LL | #![deny(fuzzy_provenance_casts)] help: use `.with_addr()` to adjust a valid pointer in the same allocation, to this address | LL | let dangling = (...).with_addr(16_usize); - | ~~~~~~~~~~~~~~~~~~~~~~~~~ + | ++++++++++++++++ ~ error: aborting due to previous error