Don't lint fat pointer to (usize, usize) conversion in transmute_undefined_repr

This commit is contained in:
Jason Newcomb 2022-03-15 22:31:07 -04:00
parent e71ac41d44
commit 442d4ce1c3
2 changed files with 21 additions and 1 deletions

View file

@ -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
}
}

View file

@ -96,5 +96,12 @@ fn main() {
let _: Ty2<u32, u32> = transmute(value::<((), Ty2<u32, u32>)>()); // Ok
let _: ((), Ty2<u32, u32>) = transmute(value::<Ty2<u32, u32>>()); // 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
}
}