Fix a fp in transmute_ptr_to_ptr

Avoid firing the lint when `transmute` in const contexts
as dereferencing raw pointers in consts is unstable. cc #5959
This commit is contained in:
rail 2020-09-01 11:21:48 +12:00
parent 67e18c2d5c
commit afeb917fca
2 changed files with 12 additions and 2 deletions

View file

@ -331,8 +331,9 @@ impl<'tcx> LateLintPass<'tcx> for Transmute {
if let Some(def_id) = cx.qpath_res(qpath, path_expr.hir_id).opt_def_id();
if match_def_path(cx, def_id, &paths::TRANSMUTE);
then {
// Avoid suggesting from/to bits in const contexts.
// Avoid suggesting from/to bits and dereferencing raw pointers in const contexts.
// See https://github.com/rust-lang/rust/issues/73736 for progress on making them `const fn`.
// And see https://github.com/rust-lang/rust/issues/51911 for dereferencing raw pointers.
let const_context = in_constant(cx, e.hir_id);
let from_ty = cx.typeck_results().expr_ty(&args[0]);
@ -486,7 +487,8 @@ impl<'tcx> LateLintPass<'tcx> for Transmute {
Applicability::Unspecified,
);
} else {
if cx.tcx.erase_regions(&from_ty) != cx.tcx.erase_regions(&to_ty) {
if (cx.tcx.erase_regions(&from_ty) != cx.tcx.erase_regions(&to_ty))
&& !const_context {
span_lint_and_then(
cx,
TRANSMUTE_PTR_TO_PTR,

View file

@ -51,4 +51,12 @@ fn transmute_ptr_to_ptr() {
let _: &GenericParam<&LifetimeParam<'static>> = unsafe { std::mem::transmute(&GenericParam { t: &lp }) };
}
// dereferencing raw pointers in const contexts, should not lint as it's unstable (issue 5959)
const _: &() = {
struct ZST;
let zst = &ZST;
unsafe { std::mem::transmute::<&'static ZST, &'static ()>(zst) }
};
fn main() {}