diff --git a/clippy_lints/src/casts/cast_ptr_alignment.rs b/clippy_lints/src/casts/cast_ptr_alignment.rs index 079b7ff06..a4ef1344a 100644 --- a/clippy_lints/src/casts/cast_ptr_alignment.rs +++ b/clippy_lints/src/casts/cast_ptr_alignment.rs @@ -1,11 +1,11 @@ use clippy_utils::diagnostics::span_lint; use clippy_utils::is_hir_ty_cfg_dependant; +use clippy_utils::ty::is_c_void; use if_chain::if_chain; use rustc_hir::{Expr, ExprKind, GenericArg}; use rustc_lint::LateContext; use rustc_middle::ty::layout::LayoutOf; use rustc_middle::ty::{self, Ty}; -use rustc_span::symbol::sym; use super::CAST_PTR_ALIGNMENT; @@ -62,19 +62,3 @@ fn lint_cast_ptr_alignment<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, cast_f } } } - -/// Check if the given type is either `core::ffi::c_void` or -/// one of the platform specific `libc::::c_void` of libc. -fn is_c_void(cx: &LateContext<'_>, ty: Ty<'_>) -> bool { - if let ty::Adt(adt, _) = ty.kind() { - let names = cx.get_def_path(adt.did); - - if names.is_empty() { - return false; - } - if names[0] == sym::libc || names[0] == sym::core && *names.last().unwrap() == sym!(c_void) { - return true; - } - } - false -} diff --git a/clippy_lints/src/transmute/transmute_undefined_repr.rs b/clippy_lints/src/transmute/transmute_undefined_repr.rs index 5b08ad584..74bdace40 100644 --- a/clippy_lints/src/transmute/transmute_undefined_repr.rs +++ b/clippy_lints/src/transmute/transmute_undefined_repr.rs @@ -1,5 +1,6 @@ use super::TRANSMUTE_UNDEFINED_REPR; use clippy_utils::diagnostics::span_lint_and_then; +use clippy_utils::ty::is_c_void; use rustc_hir::Expr; use rustc_lint::LateContext; use rustc_middle::ty::subst::{GenericArg, Subst}; @@ -100,7 +101,8 @@ pub(super) fn check<'tcx>( from_ty: from_sub_ty, to_ty: to_sub_ty, } => match (reduce_ty(cx, from_sub_ty), reduce_ty(cx, to_sub_ty)) { - (ReducedTy::IntArray, _) | (_, ReducedTy::IntArray) => return false, + (ReducedTy::IntArray | ReducedTy::TypeErasure, _) + | (_, ReducedTy::IntArray | ReducedTy::TypeErasure) => return false, (ReducedTy::UnorderedFields(from_ty), ReducedTy::UnorderedFields(to_ty)) if from_ty != to_ty => { span_lint_and_then( cx, @@ -228,8 +230,10 @@ fn reduce_refs<'tcx>( } enum ReducedTy<'tcx> { - /// The type is a struct containing either zero sized fields, or multiple sized fields with a - /// defined order. + /// The type can be used for type erasure. + TypeErasure, + /// The type is a struct containing either zero non-zero sized fields, or multiple non-zero + /// sized fields with a defined order. OrderedFields(Ty<'tcx>), /// The type is a struct containing multiple non-zero sized fields with no defined order. UnorderedFields(Ty<'tcx>), @@ -252,6 +256,7 @@ fn reduce_ty<'tcx>(cx: &LateContext<'tcx>, mut ty: Ty<'tcx>) -> ReducedTy<'tcx> ty = sub_ty; continue; }, + ty::Tuple(args) if args.is_empty() => ReducedTy::TypeErasure, ty::Tuple(args) => { let mut iter = args.iter().map(GenericArg::expect_ty); let Some(sized_ty) = iter.find(|ty| !is_zero_sized_ty(cx, ty)) else { @@ -270,7 +275,7 @@ fn reduce_ty<'tcx>(cx: &LateContext<'tcx>, mut ty: Ty<'tcx>) -> ReducedTy<'tcx> .iter() .map(|f| cx.tcx.type_of(f.did).subst(cx.tcx, substs)); let Some(sized_ty) = iter.find(|ty| !is_zero_sized_ty(cx, ty)) else { - return ReducedTy::OrderedFields(ty); + return ReducedTy::TypeErasure; }; if iter.all(|ty| is_zero_sized_ty(cx, ty)) { ty = sized_ty; @@ -282,6 +287,10 @@ fn reduce_ty<'tcx>(cx: &LateContext<'tcx>, mut ty: Ty<'tcx>) -> ReducedTy<'tcx> ReducedTy::UnorderedFields(ty) } }, + ty::Adt(def, _) if def.is_enum() && (def.variants.is_empty() || is_c_void(cx, ty)) => { + ReducedTy::TypeErasure + }, + ty::Foreign(_) => ReducedTy::TypeErasure, ty::Ref(_, ty, _) => ReducedTy::Ref(ty), ty::RawPtr(ty) => ReducedTy::Ref(ty.ty), _ => ReducedTy::Other(ty), diff --git a/clippy_utils/src/lib.rs b/clippy_utils/src/lib.rs index 73d915506..5b37a1de8 100644 --- a/clippy_utils/src/lib.rs +++ b/clippy_utils/src/lib.rs @@ -1,6 +1,7 @@ #![feature(box_patterns)] #![feature(control_flow_enum)] #![feature(let_else)] +#![feature(let_chains)] #![feature(once_cell)] #![feature(rustc_private)] #![recursion_limit = "512"] diff --git a/clippy_utils/src/ty.rs b/clippy_utils/src/ty.rs index 7d74b6990..6c9ba6452 100644 --- a/clippy_utils/src/ty.rs +++ b/clippy_utils/src/ty.rs @@ -572,3 +572,17 @@ pub fn get_discriminant_value(tcx: TyCtxt<'_>, adt: &'_ AdtDef, i: VariantIdx) - }, } } + +/// Check if the given type is either `core::ffi::c_void`, `std::os::raw::c_void`, or one of the +/// platform specific `libc::::c_void` types in libc. +pub fn is_c_void(cx: &LateContext<'_>, ty: Ty<'_>) -> bool { + if let ty::Adt(adt, _) = ty.kind() + && let &[krate, .., name] = &*cx.get_def_path(adt.did) + && let sym::libc | sym::core | sym::std = krate + && name.as_str() == "c_void" + { + true + } else { + false + } +} diff --git a/tests/ui/transmute_undefined_repr.rs b/tests/ui/transmute_undefined_repr.rs index 0916da819..c1ee0d98a 100644 --- a/tests/ui/transmute_undefined_repr.rs +++ b/tests/ui/transmute_undefined_repr.rs @@ -1,6 +1,9 @@ #![warn(clippy::transmute_undefined_repr)] #![allow(clippy::unit_arg)] +use core::ffi::c_void; +use core::mem::transmute; + fn value() -> T { unimplemented!() } @@ -14,49 +17,60 @@ struct Ty2C(T, U); fn main() { unsafe { - let _: () = core::mem::transmute(value::()); - let _: Empty = core::mem::transmute(value::<()>()); + let _: () = transmute(value::()); + let _: Empty = transmute(value::<()>()); - let _: Ty = core::mem::transmute(value::()); - let _: Ty = core::mem::transmute(value::()); + let _: Ty = transmute(value::()); + let _: Ty = transmute(value::()); - let _: Ty2C = core::mem::transmute(value::>()); // Lint, Ty2 is unordered - let _: Ty2 = core::mem::transmute(value::>()); // Lint, Ty2 is unordered + let _: Ty2C = transmute(value::>()); // Lint, Ty2 is unordered + let _: Ty2 = transmute(value::>()); // Lint, Ty2 is unordered - let _: Ty2 = core::mem::transmute(value::>>()); // Ok, Ty2 types are the same - let _: Ty> = core::mem::transmute(value::>()); // Ok, Ty2 types are the same + let _: Ty2 = transmute(value::>>()); // Ok, Ty2 types are the same + let _: Ty> = transmute(value::>()); // Ok, Ty2 types are the same - let _: Ty2 = core::mem::transmute(value::>>()); // Lint, different Ty2 instances - let _: Ty> = core::mem::transmute(value::>()); // Lint, different Ty2 instances + let _: Ty2 = transmute(value::>>()); // Lint, different Ty2 instances + let _: Ty> = transmute(value::>()); // Lint, different Ty2 instances - let _: Ty<&()> = core::mem::transmute(value::<&()>()); - let _: &() = core::mem::transmute(value::>()); + let _: Ty<&()> = transmute(value::<&()>()); + let _: &() = transmute(value::>()); - let _: &Ty2 = core::mem::transmute(value::>>()); // Lint, different Ty2 instances - let _: Ty<&Ty2> = core::mem::transmute(value::<&Ty2>()); // Lint, different Ty2 instances + let _: &Ty2 = transmute(value::>>()); // Lint, different Ty2 instances + let _: Ty<&Ty2> = transmute(value::<&Ty2>()); // Lint, different Ty2 instances - let _: Ty = core::mem::transmute(value::<&Ty2>()); // Ok, pointer to usize conversion - let _: &Ty2 = core::mem::transmute(value::>()); // Ok, pointer to usize conversion + let _: Ty = transmute(value::<&Ty2>()); // Ok, pointer to usize conversion + let _: &Ty2 = transmute(value::>()); // Ok, pointer to usize conversion - let _: Ty<[u8; 8]> = core::mem::transmute(value::>()); // Ok, transmute to byte array - let _: Ty2 = core::mem::transmute(value::>()); // Ok, transmute from byte array + let _: Ty<[u8; 8]> = transmute(value::>()); // Ok, transmute to byte array + let _: Ty2 = transmute(value::>()); // Ok, transmute from byte array // 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 + let _: Ty2C, ()> = transmute(value::>()); // Ok, Ty2 types are the same + let _: Ty2 = 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 + let _: &'static mut Ty2 = transmute(value::>>()); // Ok, Ty2 types are the same + let _: Box> = transmute(value::<&'static mut Ty2>()); // Ok, Ty2 types are the same + let _: *mut Ty2 = transmute(value::>>()); // Ok, Ty2 types are the same + let _: Box> = 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 + let _: &'static mut Ty2 = transmute(value::>>()); // Lint, different Ty2 instances + let _: Box> = transmute(value::<&'static mut Ty2>()); // Lint, different Ty2 instances + let _: *const () = transmute(value::>>()); // Ok, type erasure + let _: Ty<&Ty2> = transmute(value::<*const ()>()); // Ok, reverse type erasure + let _: *const c_void = transmute(value::>>()); // Ok, type erasure + let _: Ty<&Ty2> = transmute(value::<*const c_void>()); // Ok, reverse type erasure + + enum Erase {} + let _: *const Erase = transmute(value::>>()); // Ok, type erasure + let _: Ty<&Ty2> = transmute(value::<*const Erase>()); // Ok, reverse type erasure + + struct Erase2( + [u8; 0], + core::marker::PhantomData<(*mut u8, core::marker::PhantomPinned)>, + ); + let _: *const Erase2 = transmute(value::>>()); // Ok, type erasure + let _: Ty<&Ty2> = transmute(value::<*const Erase2>()); // Ok, reverse type erasure } } diff --git a/tests/ui/transmute_undefined_repr.stderr b/tests/ui/transmute_undefined_repr.stderr index 98035305e..42d544fc9 100644 --- a/tests/ui/transmute_undefined_repr.stderr +++ b/tests/ui/transmute_undefined_repr.stderr @@ -1,62 +1,62 @@ error: transmute from `Ty2` which has an undefined layout - --> $DIR/transmute_undefined_repr.rs:23:33 + --> $DIR/transmute_undefined_repr.rs:26:33 | -LL | let _: Ty2C = core::mem::transmute(value::>()); // Lint, Ty2 is unordered - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +LL | let _: Ty2C = transmute(value::>()); // Lint, Ty2 is unordered + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: `-D clippy::transmute-undefined-repr` implied by `-D warnings` error: transmute into `Ty2` which has an undefined layout - --> $DIR/transmute_undefined_repr.rs:24:32 + --> $DIR/transmute_undefined_repr.rs:27:32 | -LL | let _: Ty2 = core::mem::transmute(value::>()); // Lint, Ty2 is unordered - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +LL | let _: Ty2 = transmute(value::>()); // Lint, Ty2 is unordered + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: transmute from `Ty>` to `Ty2`, both of which have an undefined layout - --> $DIR/transmute_undefined_repr.rs:29:32 + --> $DIR/transmute_undefined_repr.rs:32:32 | -LL | let _: Ty2 = core::mem::transmute(value::>>()); // Lint, different Ty2 instances - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +LL | let _: Ty2 = transmute(value::>>()); // Lint, different Ty2 instances + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: two instances of the same generic type (`Ty2`) may have different layouts error: transmute from `Ty2` to `Ty>`, both of which have an undefined layout - --> $DIR/transmute_undefined_repr.rs:30:36 + --> $DIR/transmute_undefined_repr.rs:33:36 | -LL | let _: Ty> = core::mem::transmute(value::>()); // Lint, different Ty2 instances - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +LL | let _: Ty> = transmute(value::>()); // Lint, different Ty2 instances + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: two instances of the same generic type (`Ty2`) may have different layouts error: transmute from `Ty<&Ty2>` to `&Ty2`, both of which have an undefined layout - --> $DIR/transmute_undefined_repr.rs:35:33 + --> $DIR/transmute_undefined_repr.rs:38:33 | -LL | let _: &Ty2 = core::mem::transmute(value::>>()); // Lint, different Ty2 instances - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +LL | let _: &Ty2 = transmute(value::>>()); // Lint, different Ty2 instances + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: two instances of the same generic type (`Ty2`) may have different layouts error: transmute from `&Ty2` to `Ty<&Ty2>`, both of which have an undefined layout - --> $DIR/transmute_undefined_repr.rs:36:37 + --> $DIR/transmute_undefined_repr.rs:39:37 | -LL | let _: Ty<&Ty2> = core::mem::transmute(value::<&Ty2>()); // Lint, different Ty2 instances - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +LL | let _: Ty<&Ty2> = transmute(value::<&Ty2>()); // Lint, different Ty2 instances + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: two instances of the same generic type (`Ty2`) may have different layouts 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 - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +LL | let _: &'static mut Ty2 = transmute(value::>>()); // Lint, different Ty2 instances + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = 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 + --> $DIR/transmute_undefined_repr.rs:57:37 | -LL | let _: Box> = core::mem::transmute(value::<&'static mut Ty2>()); // Lint - | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +LL | let _: Box> = transmute(value::<&'static mut Ty2>()); // Lint, different Ty2 instances + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: two instances of the same generic type (`Ty2`) may have different layouts