Skip to content

Commit

Permalink
Fix transmute_undefined_repr when converting between a fat pointer …
Browse files Browse the repository at this point in the history
…and a type containing a fat pointer
  • Loading branch information
Jarcho committed Feb 18, 2022
1 parent 7c07022 commit d28d19d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
20 changes: 18 additions & 2 deletions clippy_lints/src/transmute/transmute_undefined_repr.rs
Expand Up @@ -19,8 +19,16 @@ pub(super) fn check<'tcx>(

while from_ty != to_ty {
match reduce_refs(cx, e.span, from_ty, to_ty) {
ReducedTys::FromFatPtr { unsized_ty, to_ty } => match reduce_ty(cx, to_ty) {
ReducedTys::FromFatPtr {
unsized_ty,
to_ty: to_sub_ty,
} => match reduce_ty(cx, to_sub_ty) {
ReducedTy::IntArray | ReducedTy::TypeErasure => break,
ReducedTy::Ref(to_sub_ty) => {
from_ty = unsized_ty;
to_ty = to_sub_ty;
continue;
},
_ => {
span_lint_and_then(
cx,
Expand All @@ -36,8 +44,16 @@ pub(super) fn check<'tcx>(
return true;
},
},
ReducedTys::ToFatPtr { unsized_ty, from_ty } => match reduce_ty(cx, from_ty) {
ReducedTys::ToFatPtr {
unsized_ty,
from_ty: from_sub_ty,
} => match reduce_ty(cx, from_sub_ty) {
ReducedTy::IntArray | ReducedTy::TypeErasure => break,
ReducedTy::Ref(from_sub_ty) => {
from_ty = from_sub_ty;
to_ty = unsized_ty;
continue;
},
_ => {
span_lint_and_then(
cx,
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/transmute_undefined_repr.rs
Expand Up @@ -84,5 +84,8 @@ fn main() {

let _: [usize; 2] = transmute(value::<&[u8]>()); // Ok, transmute to int array
let _: &[u8] = transmute(value::<[usize; 2]>()); // Ok, transmute from int array

let _: *const [u8] = transmute(value::<Box<[u8]>>()); // Ok
let _: Box<[u8]> = transmute(value::<*mut [u8]>()); // Ok
}
}

0 comments on commit d28d19d

Please sign in to comment.