Skip to content

Commit 1d63d6d

Browse files
committed
Improve fuzzy_provenance_casts lint diagnostics
Use `multipart_suggestion` instead of getting a snippet.
1 parent 02d12bc commit 1d63d6d

File tree

2 files changed

+7
-11
lines changed

2 files changed

+7
-11
lines changed

compiler/rustc_typeck/src/check/cast.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -1041,16 +1041,12 @@ impl<'a, 'tcx> CastCheck<'tcx> {
10411041
self.expr_ty, self.cast_ty
10421042
));
10431043
let msg = "use `.with_addr()` to adjust a valid pointer in the same allocation, to this address";
1044-
if let Ok(snippet) = fcx.tcx.sess.source_map().span_to_snippet(self.expr_span) {
1045-
err.span_suggestion(
1046-
self.span,
1047-
msg,
1048-
format!("(...).with_addr({snippet})"),
1049-
Applicability::HasPlaceholders,
1050-
);
1051-
} else {
1052-
err.help(msg);
1053-
}
1044+
let suggestions = vec![
1045+
(self.expr_span.shrink_to_lo(), String::from("(...).with_addr(")),
1046+
(self.expr_span.shrink_to_hi().to(self.cast_span), String::from(")")),
1047+
];
1048+
1049+
err.multipart_suggestion(msg, suggestions, Applicability::MaybeIncorrect);
10541050
err.help(
10551051
"if you can't comply with strict provenance and don't have a pointer with \
10561052
the correct provenance you can use `std::ptr::from_exposed_addr()` instead"

src/test/ui/lint/lint-strict-provenance-fuzzy-casts.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ LL | #![deny(fuzzy_provenance_casts)]
1313
help: use `.with_addr()` to adjust a valid pointer in the same allocation, to this address
1414
|
1515
LL | let dangling = (...).with_addr(16_usize);
16-
| ~~~~~~~~~~~~~~~~~~~~~~~~~
16+
| ++++++++++++++++ ~
1717

1818
error: aborting due to previous error
1919

0 commit comments

Comments
 (0)