Skip to content

Commit 442d4ce

Browse files
committed
Don't lint fat pointer to (usize, usize) conversion in transmute_undefined_repr
1 parent e71ac41 commit 442d4ce

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

clippy_lints/src/transmute/transmute_undefined_repr.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use clippy_utils::ty::is_c_void;
44
use rustc_hir::Expr;
55
use rustc_lint::LateContext;
66
use rustc_middle::ty::subst::Subst;
7-
use rustc_middle::ty::{self, Ty, TypeAndMut};
7+
use rustc_middle::ty::{self, IntTy, Ty, TypeAndMut, UintTy};
88
use rustc_span::Span;
99

1010
#[allow(clippy::too_many_lines)]
@@ -24,6 +24,7 @@ pub(super) fn check<'tcx>(
2424
to_ty: to_sub_ty,
2525
} => match reduce_ty(cx, to_sub_ty) {
2626
ReducedTy::IntArray | ReducedTy::TypeErasure => break,
27+
ReducedTy::UnorderedFields(ty) if is_size_pair(ty) => break,
2728
ReducedTy::Ref(to_sub_ty) => {
2829
from_ty = unsized_ty;
2930
to_ty = to_sub_ty;
@@ -49,6 +50,7 @@ pub(super) fn check<'tcx>(
4950
from_ty: from_sub_ty,
5051
} => match reduce_ty(cx, from_sub_ty) {
5152
ReducedTy::IntArray | ReducedTy::TypeErasure => break,
53+
ReducedTy::UnorderedFields(ty) if is_size_pair(ty) => break,
5254
ReducedTy::Ref(from_sub_ty) => {
5355
from_ty = from_sub_ty;
5456
to_ty = unsized_ty;
@@ -333,3 +335,14 @@ fn is_zero_sized_ty<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
333335
}
334336
}
335337
}
338+
339+
fn is_size_pair(ty: Ty<'_>) -> bool {
340+
if let ty::Tuple(tys) = *ty.kind()
341+
&& let [ty1, ty2] = &**tys
342+
{
343+
matches!(ty1.kind(), ty::Int(IntTy::Isize) | ty::Uint(UintTy::Usize))
344+
&& matches!(ty2.kind(), ty::Int(IntTy::Isize) | ty::Uint(UintTy::Usize))
345+
} else {
346+
false
347+
}
348+
}

tests/ui/transmute_undefined_repr.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,12 @@ fn main() {
9696

9797
let _: Ty2<u32, u32> = transmute(value::<((), Ty2<u32, u32>)>()); // Ok
9898
let _: ((), Ty2<u32, u32>) = transmute(value::<Ty2<u32, u32>>()); // Ok
99+
100+
let _: (usize, usize) = transmute(value::<&[u8]>()); // Ok
101+
let _: &[u8] = transmute(value::<(usize, usize)>()); // Ok
102+
103+
trait Trait {}
104+
let _: (isize, isize) = transmute(value::<&dyn Trait>()); // Ok
105+
let _: &dyn Trait = transmute(value::<(isize, isize)>()); // Ok
99106
}
100107
}

0 commit comments

Comments
 (0)