diff options
| author | Samuel Tardieu <sam@rfc1149.net> | 2024-03-03 10:32:39 +0100 |
|---|---|---|
| committer | Samuel Tardieu <sam@rfc1149.net> | 2024-03-03 23:55:01 +0100 |
| commit | 6e5332cd9cc94dcaf0173b8064fa6778718260ec (patch) | |
| tree | 0332c8b309b545bc7d4fb1c830f82373ff525924 | |
| parent | 28e11b31deef5fd0f59a1e289f8f2173ed6e5944 (diff) | |
| download | rust-6e5332cd9cc94dcaf0173b8064fa6778718260ec.tar.gz rust-6e5332cd9cc94dcaf0173b8064fa6778718260ec.zip | |
Pointers cannot be converted to integers at compile time
4 files changed, 17 insertions, 1 deletions
diff --git a/clippy_lints/src/transmute/mod.rs b/clippy_lints/src/transmute/mod.rs index 06de7a11031..e47b14bf63b 100644 --- a/clippy_lints/src/transmute/mod.rs +++ b/clippy_lints/src/transmute/mod.rs @@ -592,7 +592,7 @@ impl<'tcx> LateLintPass<'tcx> for Transmute { | (eager_transmute::check(cx, e, arg, from_ty, to_ty)); if !linted { - transmutes_expressible_as_ptr_casts::check(cx, e, from_ty, from_ty_adjusted, to_ty, arg); + transmutes_expressible_as_ptr_casts::check(cx, e, from_ty, from_ty_adjusted, to_ty, arg, const_context); } } } diff --git a/clippy_lints/src/transmute/transmutes_expressible_as_ptr_casts.rs b/clippy_lints/src/transmute/transmutes_expressible_as_ptr_casts.rs index bbecc39a813..043c9c88601 100644 --- a/clippy_lints/src/transmute/transmutes_expressible_as_ptr_casts.rs +++ b/clippy_lints/src/transmute/transmutes_expressible_as_ptr_casts.rs @@ -18,10 +18,12 @@ pub(super) fn check<'tcx>( from_ty_adjusted: bool, to_ty: Ty<'tcx>, arg: &'tcx Expr<'_>, + const_context: bool, ) -> bool { use CastKind::{AddrPtrCast, ArrayPtrCast, FnPtrAddrCast, FnPtrPtrCast, PtrAddrCast, PtrPtrCast}; let mut app = Applicability::MachineApplicable; let mut sugg = match check_cast(cx, e, from_ty, to_ty) { + Some(FnPtrAddrCast | PtrAddrCast) if const_context => return false, Some(PtrPtrCast | AddrPtrCast | ArrayPtrCast | FnPtrPtrCast | FnPtrAddrCast) => { Sugg::hir_with_context(cx, arg, e.span.ctxt(), "..", &mut app) .as_ty(to_ty.to_string()) diff --git a/tests/ui/transmutes_expressible_as_ptr_casts.fixed b/tests/ui/transmutes_expressible_as_ptr_casts.fixed index 08b8e786611..2365695d691 100644 --- a/tests/ui/transmutes_expressible_as_ptr_casts.fixed +++ b/tests/ui/transmutes_expressible_as_ptr_casts.fixed @@ -82,3 +82,10 @@ fn issue_10449() { let _x: u8 = unsafe { *(f as *const u8) }; } + +// Pointers cannot be cast to integers in const contexts +const fn issue_12402<P>(ptr: *const P) { + unsafe { transmute::<*const i32, usize>(&42i32) }; + unsafe { transmute::<fn(*const P), usize>(issue_12402) }; + let _ = unsafe { transmute::<_, usize>(ptr) }; +} diff --git a/tests/ui/transmutes_expressible_as_ptr_casts.rs b/tests/ui/transmutes_expressible_as_ptr_casts.rs index 92eb765e5f9..cd1607b4c19 100644 --- a/tests/ui/transmutes_expressible_as_ptr_casts.rs +++ b/tests/ui/transmutes_expressible_as_ptr_casts.rs @@ -82,3 +82,10 @@ fn issue_10449() { let _x: u8 = unsafe { *std::mem::transmute::<fn(), *const u8>(f) }; } + +// Pointers cannot be cast to integers in const contexts +const fn issue_12402<P>(ptr: *const P) { + unsafe { transmute::<*const i32, usize>(&42i32) }; + unsafe { transmute::<fn(*const P), usize>(issue_12402) }; + let _ = unsafe { transmute::<_, usize>(ptr) }; +} |
