Skip to content

Commit 48248af

Browse files
committed
Auto merge of rust-lang#5999 - rail-rain:fix_fp_transmute_ptr_to_ptr_in_consts, r=ebroto
Fix a fp in `transmute_ptr_to_ptr` fixes rust-lang#5959 changelog: Fix a false positive in `transmute_ptr_to_ptr` that the lint fires when `transmute` is used to cast a reference in const contexts although dereferencing raw pointers in consts is unstable.
2 parents 67e18c2 + afeb917 commit 48248af

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

clippy_lints/src/transmute.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,9 @@ impl<'tcx> LateLintPass<'tcx> for Transmute {
331331
if let Some(def_id) = cx.qpath_res(qpath, path_expr.hir_id).opt_def_id();
332332
if match_def_path(cx, def_id, &paths::TRANSMUTE);
333333
then {
334-
// Avoid suggesting from/to bits in const contexts.
334+
// Avoid suggesting from/to bits and dereferencing raw pointers in const contexts.
335335
// See https://github.com/rust-lang/rust/issues/73736 for progress on making them `const fn`.
336+
// And see https://github.com/rust-lang/rust/issues/51911 for dereferencing raw pointers.
336337
let const_context = in_constant(cx, e.hir_id);
337338

338339
let from_ty = cx.typeck_results().expr_ty(&args[0]);
@@ -486,7 +487,8 @@ impl<'tcx> LateLintPass<'tcx> for Transmute {
486487
Applicability::Unspecified,
487488
);
488489
} else {
489-
if cx.tcx.erase_regions(&from_ty) != cx.tcx.erase_regions(&to_ty) {
490+
if (cx.tcx.erase_regions(&from_ty) != cx.tcx.erase_regions(&to_ty))
491+
&& !const_context {
490492
span_lint_and_then(
491493
cx,
492494
TRANSMUTE_PTR_TO_PTR,

tests/ui/transmute_ptr_to_ptr.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,12 @@ fn transmute_ptr_to_ptr() {
5151
let _: &GenericParam<&LifetimeParam<'static>> = unsafe { std::mem::transmute(&GenericParam { t: &lp }) };
5252
}
5353

54+
// dereferencing raw pointers in const contexts, should not lint as it's unstable (issue 5959)
55+
const _: &() = {
56+
struct ZST;
57+
let zst = &ZST;
58+
59+
unsafe { std::mem::transmute::<&'static ZST, &'static ()>(zst) }
60+
};
61+
5462
fn main() {}

0 commit comments

Comments
 (0)