From 442d4ce1c36011a8b12fb3ce9a4d70a7031fc4d5 Mon Sep 17 00:00:00 2001 From: Jason Newcomb Date: Tue, 15 Mar 2022 22:31:07 -0400 Subject: [PATCH] Don't lint fat pointer to `(usize, usize)` conversion in `transmute_undefined_repr` --- .../src/transmute/transmute_undefined_repr.rs | 15 ++++++++++++++- tests/ui/transmute_undefined_repr.rs | 7 +++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/clippy_lints/src/transmute/transmute_undefined_repr.rs b/clippy_lints/src/transmute/transmute_undefined_repr.rs index 3cd9d208943..30059e5ba2b 100644 --- a/clippy_lints/src/transmute/transmute_undefined_repr.rs +++ b/clippy_lints/src/transmute/transmute_undefined_repr.rs @@ -4,7 +4,7 @@ use clippy_utils::ty::is_c_void; use rustc_hir::Expr; use rustc_lint::LateContext; use rustc_middle::ty::subst::Subst; -use rustc_middle::ty::{self, Ty, TypeAndMut}; +use rustc_middle::ty::{self, IntTy, Ty, TypeAndMut, UintTy}; use rustc_span::Span; #[allow(clippy::too_many_lines)] @@ -24,6 +24,7 @@ pub(super) fn check<'tcx>( to_ty: to_sub_ty, } => match reduce_ty(cx, to_sub_ty) { ReducedTy::IntArray | ReducedTy::TypeErasure => break, + ReducedTy::UnorderedFields(ty) if is_size_pair(ty) => break, ReducedTy::Ref(to_sub_ty) => { from_ty = unsized_ty; to_ty = to_sub_ty; @@ -49,6 +50,7 @@ pub(super) fn check<'tcx>( from_ty: from_sub_ty, } => match reduce_ty(cx, from_sub_ty) { ReducedTy::IntArray | ReducedTy::TypeErasure => break, + ReducedTy::UnorderedFields(ty) if is_size_pair(ty) => break, ReducedTy::Ref(from_sub_ty) => { from_ty = from_sub_ty; to_ty = unsized_ty; @@ -333,3 +335,14 @@ fn is_zero_sized_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool { } } } + +fn is_size_pair(ty: Ty<'_>) -> bool { + if let ty::Tuple(tys) = *ty.kind() + && let [ty1, ty2] = &**tys + { + matches!(ty1.kind(), ty::Int(IntTy::Isize) | ty::Uint(UintTy::Usize)) + && matches!(ty2.kind(), ty::Int(IntTy::Isize) | ty::Uint(UintTy::Usize)) + } else { + false + } +} diff --git a/tests/ui/transmute_undefined_repr.rs b/tests/ui/transmute_undefined_repr.rs index e7a59dbed95..13d8cb10e98 100644 --- a/tests/ui/transmute_undefined_repr.rs +++ b/tests/ui/transmute_undefined_repr.rs @@ -96,5 +96,12 @@ fn main() { let _: Ty2 = transmute(value::<((), Ty2)>()); // Ok let _: ((), Ty2) = transmute(value::>()); // Ok + + let _: (usize, usize) = transmute(value::<&[u8]>()); // Ok + let _: &[u8] = transmute(value::<(usize, usize)>()); // Ok + + trait Trait {} + let _: (isize, isize) = transmute(value::<&dyn Trait>()); // Ok + let _: &dyn Trait = transmute(value::<(isize, isize)>()); // Ok } }