From c02dff716785721c4c992d03ee816d764fb43402 Mon Sep 17 00:00:00 2001 From: Jason Newcomb Date: Tue, 15 Feb 2022 11:29:02 -0500 Subject: [PATCH] Fix `transmute_undefined_repr` when converting between a pointer and a type containing a pointer --- .../src/transmute/transmute_undefined_repr.rs | 3 ++- tests/ui/transmute_undefined_repr.rs | 14 ++++++++++ tests/ui/transmute_undefined_repr.stderr | 26 ++++++++++++++++--- 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/clippy_lints/src/transmute/transmute_undefined_repr.rs b/clippy_lints/src/transmute/transmute_undefined_repr.rs index 89439862f..74ea7a3b8 100644 --- a/clippy_lints/src/transmute/transmute_undefined_repr.rs +++ b/clippy_lints/src/transmute/transmute_undefined_repr.rs @@ -273,7 +273,8 @@ fn reduce_ty<'tcx>(cx: &LateContext<'tcx>, mut ty: Ty<'tcx>) -> ReducedTy<'tcx> ReducedTy::UnorderedFields(ty) } }, - ty::Ref(..) | ty::RawPtr(_) => ReducedTy::Ref(ty), + ty::Ref(_, ty, _) => ReducedTy::Ref(ty), + ty::RawPtr(ty) => ReducedTy::Ref(ty.ty), _ => ReducedTy::Other(ty), }; } diff --git a/tests/ui/transmute_undefined_repr.rs b/tests/ui/transmute_undefined_repr.rs index f0383a03f..0916da819 100644 --- a/tests/ui/transmute_undefined_repr.rs +++ b/tests/ui/transmute_undefined_repr.rs @@ -44,5 +44,19 @@ fn main() { // issue #8417 let _: Ty2C, ()> = core::mem::transmute(value::>()); // Ok, Ty2 types are the same let _: Ty2 = core::mem::transmute(value::, ()>>()); // Ok, Ty2 types are the same + + // Ty2 types are the same + let _: &'static mut Ty2 = core::mem::transmute(value::>>()); // Ok + // Ty2 types are the same + let _: Box> = core::mem::transmute(value::<&'static mut Ty2>()); // Ok + let _: *mut Ty2 = core::mem::transmute(value::>>()); // Ok, Ty2 types are the same + let _: Box> = core::mem::transmute(value::<*mut Ty2>()); // Ok, Ty2 types are the same + + // Different Ty2 instances + let _: &'static mut Ty2 = core::mem::transmute(value::>>()); // Lint + // Different Ty2 instances + let _: Box> = core::mem::transmute(value::<&'static mut Ty2>()); // Lint + + } } diff --git a/tests/ui/transmute_undefined_repr.stderr b/tests/ui/transmute_undefined_repr.stderr index 040c63c7a..98035305e 100644 --- a/tests/ui/transmute_undefined_repr.stderr +++ b/tests/ui/transmute_undefined_repr.stderr @@ -28,17 +28,37 @@ LL | let _: Ty> = core::mem::transmute(value::` which has an undefined layout +error: transmute from `Ty<&Ty2>` to `&Ty2`, both of which have an undefined layout --> $DIR/transmute_undefined_repr.rs:35:33 | LL | let _: &Ty2 = core::mem::transmute(value::>>()); // Lint, different Ty2 instances | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: two instances of the same generic type (`Ty2`) may have different layouts -error: transmute from `&Ty2` which has an undefined layout +error: transmute from `&Ty2` to `Ty<&Ty2>`, both of which have an undefined layout --> $DIR/transmute_undefined_repr.rs:36:37 | LL | let _: Ty<&Ty2> = core::mem::transmute(value::<&Ty2>()); // Lint, different Ty2 instances | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: two instances of the same generic type (`Ty2`) may have different layouts -error: aborting due to 6 previous errors +error: transmute from `std::boxed::Box>` to `&mut Ty2`, both of which have an undefined layout + --> $DIR/transmute_undefined_repr.rs:56:45 + | +LL | let _: &'static mut Ty2 = core::mem::transmute(value::>>()); // Lint + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: two instances of the same generic type (`Ty2`) may have different layouts + +error: transmute from `&mut Ty2` to `std::boxed::Box>`, both of which have an undefined layout + --> $DIR/transmute_undefined_repr.rs:58:37 + | +LL | let _: Box> = core::mem::transmute(value::<&'static mut Ty2>()); // Lint + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + | + = note: two instances of the same generic type (`Ty2`) may have different layouts + +error: aborting due to 8 previous errors