diff options
| author | niluxv <niluxv.opensource.C-h2ty6xl@yandex.com> | 2022-04-16 10:18:31 +0200 |
|---|---|---|
| committer | niluxv <niluxv.opensource.C-h2ty6xl@yandex.com> | 2022-04-16 10:40:06 +0200 |
| commit | 1d63d6db558b1ff6cd02cb471b51957657c2d71f (patch) | |
| tree | f13c63a31bcc80aa51653d20a79f227799b6fe4f | |
| parent | 02d12bc30c173fd0cbf55d67c42d6cbae231203a (diff) | |
| download | rust-1d63d6db558b1ff6cd02cb471b51957657c2d71f.tar.gz rust-1d63d6db558b1ff6cd02cb471b51957657c2d71f.zip | |
Improve `fuzzy_provenance_casts` lint diagnostics
Use `multipart_suggestion` instead of getting a snippet.
| -rw-r--r-- | compiler/rustc_typeck/src/check/cast.rs | 16 | ||||
| -rw-r--r-- | src/test/ui/lint/lint-strict-provenance-fuzzy-casts.stderr | 2 |
2 files changed, 7 insertions, 11 deletions
diff --git a/compiler/rustc_typeck/src/check/cast.rs b/compiler/rustc_typeck/src/check/cast.rs index da054077376..a153997599a 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 e50d243b6ad..c85934aa3ba 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 |
