Skip to content

feat: lint against transmute int to ptr #13192

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions clippy_lints/src/transmute/wrong_transmute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,18 @@ use rustc_middle::ty::{self, Ty};

/// Checks for `wrong_transmute` lint.
/// Returns `true` if it's triggered, otherwise returns `false`.
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>, from_ty: Ty<'tcx>, to_ty: Ty<'tcx>) -> bool {
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>, mut from_ty: Ty<'tcx>, to_ty: Ty<'tcx>) -> bool {
if let ty::Adt(def, args) = from_ty.kind() {
let mut fields = def.variants().iter().filter_map(|v| non_zst_inner_ty(cx, v));
if fields.clone().count() != 1 {
return false;
}
if let Some(field) = fields.next_back() {
from_ty = field.ty(cx.tcx, args);
}
}
match (&from_ty.kind(), &to_ty.kind()) {
(ty::Float(_) | ty::Char, ty::Ref(..) | ty::RawPtr(_, _)) => {
(ty::Uint(_) | ty::Int(_) | ty::Float(_) | ty::Char, ty::Ref(..) | ty::RawPtr(_, _)) => {
span_lint(
cx,
WRONG_TRANSMUTE,
Expand All @@ -20,3 +29,15 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>, from_ty: Ty
_ => false,
}
}

fn non_zst_inner_ty<'a>(cx: &LateContext<'_>, variant: &'a ty::VariantDef) -> Option<&'a ty::FieldDef> {
let tcx = cx.tcx;
let param_env = tcx.param_env(variant.def_id);
variant.fields.iter().find(|field| {
let field_ty = tcx.type_of(field.did).instantiate_identity();
let is_1zst = tcx
.layout_of(param_env.and(field_ty))
.is_ok_and(|layout| layout.is_1zst());
!is_1zst
})
}
26 changes: 26 additions & 0 deletions tests/ui/transmute_int_to_ptr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#![warn(clippy::wrong_transmute)]

#[repr(transparent)]
#[allow(dead_code)]
pub struct K1 {
b: usize,
c: (),
}

pub fn foo(k: K1) {
let x = unsafe { std::mem::transmute::<K1, *mut i8>(k) };
std::hint::black_box(unsafe { *x });
}

#[allow(dead_code)]
pub struct K2 {
b: usize,
c: (),
}

pub fn bar(k: K2) {
let x = unsafe { std::mem::transmute::<K2, *mut i8>(k) };
std::hint::black_box(unsafe { *x });
}

fn main() {}
17 changes: 17 additions & 0 deletions tests/ui/transmute_int_to_ptr.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
error: transmute from a `usize` to a pointer
--> tests/ui/transmute_int_to_ptr.rs:11:22
|
LL | let x = unsafe { std::mem::transmute::<K1, *mut i8>(k) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::wrong-transmute` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::wrong_transmute)]`
Comment on lines +1 to +8
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be a new lint with more specific diagnostics mentioning that it's UB to dereference the pointer


error: transmute from a `usize` to a pointer
--> tests/ui/transmute_int_to_ptr.rs:22:22
|
LL | let x = unsafe { std::mem::transmute::<K2, *mut i8>(k) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 2 previous errors

Loading