2020-08-03 00:41:50 +00:00
|
|
|
#![warn(clippy::transmutes_expressible_as_ptr_casts)]
|
2022-07-18 07:39:37 +00:00
|
|
|
// These two warnings currently cover the cases transmutes_expressible_as_ptr_casts
|
2020-08-03 08:47:25 +00:00
|
|
|
// would otherwise be responsible for
|
|
|
|
#![warn(clippy::useless_transmute)]
|
|
|
|
#![warn(clippy::transmute_ptr_to_ptr)]
|
2024-02-19 13:53:53 +00:00
|
|
|
#![allow(unused, clippy::borrow_as_ptr, clippy::missing_transmute_annotations)]
|
2020-08-03 08:47:25 +00:00
|
|
|
|
2020-08-09 06:15:56 +00:00
|
|
|
use std::mem::{size_of, transmute};
|
2020-08-03 00:41:50 +00:00
|
|
|
|
2022-09-26 11:00:29 +00:00
|
|
|
// rustc_hir_analysis::check::cast contains documentation about when a cast `e as U` is
|
2020-08-03 05:17:11 +00:00
|
|
|
// valid, which we quote from below.
|
2020-08-03 00:41:50 +00:00
|
|
|
fn main() {
|
2020-08-03 08:47:25 +00:00
|
|
|
// We should see an error message for each transmute, and no error messages for
|
|
|
|
// the casts, since the casts are the recommended fixes.
|
|
|
|
|
2020-08-03 05:17:11 +00:00
|
|
|
// e is an integer and U is *U_0, while U_0: Sized; addr-ptr-cast
|
2020-08-11 13:43:21 +00:00
|
|
|
let _ptr_i32_transmute = unsafe { transmute::<usize, *const i32>(usize::MAX) };
|
2020-08-09 06:28:56 +00:00
|
|
|
let ptr_i32 = usize::MAX as *const i32;
|
2020-08-03 05:17:11 +00:00
|
|
|
|
|
|
|
// e has type *T, U is *U_0, and either U_0: Sized ...
|
2020-08-11 13:43:21 +00:00
|
|
|
let _ptr_i8_transmute = unsafe { transmute::<*const i32, *const i8>(ptr_i32) };
|
2020-08-09 06:28:56 +00:00
|
|
|
let _ptr_i8 = ptr_i32 as *const i8;
|
2020-08-03 05:17:11 +00:00
|
|
|
|
2020-08-11 13:43:21 +00:00
|
|
|
let slice_ptr = &[0, 1, 2, 3] as *const [i32];
|
2020-08-03 05:17:11 +00:00
|
|
|
|
|
|
|
// ... or pointer_kind(T) = pointer_kind(U_0); ptr-ptr-cast
|
2022-03-14 11:02:53 +00:00
|
|
|
let _ptr_to_unsized_transmute = unsafe { transmute::<*const [i32], *const [u32]>(slice_ptr) };
|
|
|
|
let _ptr_to_unsized = slice_ptr as *const [u32];
|
2020-08-03 05:17:11 +00:00
|
|
|
// TODO: We could try testing vtable casts here too, but maybe
|
|
|
|
// we should wait until std::raw::TraitObject is stabilized?
|
|
|
|
|
|
|
|
// e has type *T and U is a numeric type, while T: Sized; ptr-addr-cast
|
2020-08-11 13:43:21 +00:00
|
|
|
let _usize_from_int_ptr_transmute = unsafe { transmute::<*const i32, usize>(ptr_i32) };
|
2020-08-09 06:28:56 +00:00
|
|
|
let _usize_from_int_ptr = ptr_i32 as usize;
|
2020-08-03 05:17:11 +00:00
|
|
|
|
2020-08-11 13:43:21 +00:00
|
|
|
let array_ref: &[i32; 4] = &[1, 2, 3, 4];
|
2020-08-03 05:17:11 +00:00
|
|
|
|
|
|
|
// e has type &[T; n] and U is *const T; array-ptr-cast
|
2020-08-11 13:43:21 +00:00
|
|
|
let _array_ptr_transmute = unsafe { transmute::<&[i32; 4], *const [i32; 4]>(array_ref) };
|
2020-08-09 06:28:56 +00:00
|
|
|
let _array_ptr = array_ref as *const [i32; 4];
|
2020-08-03 05:17:11 +00:00
|
|
|
|
2020-08-11 13:43:21 +00:00
|
|
|
fn foo(_: usize) -> u8 {
|
|
|
|
42
|
|
|
|
}
|
2020-08-03 05:17:11 +00:00
|
|
|
|
|
|
|
// e is a function pointer type and U has type *T, while T: Sized; fptr-ptr-cast
|
2020-08-11 13:43:21 +00:00
|
|
|
let _usize_ptr_transmute = unsafe { transmute::<fn(usize) -> u8, *const usize>(foo) };
|
2020-08-09 06:28:56 +00:00
|
|
|
let _usize_ptr_transmute = foo as *const usize;
|
2020-08-03 05:17:11 +00:00
|
|
|
|
|
|
|
// e is a function pointer type and U is an integer; fptr-addr-cast
|
2020-08-11 13:43:21 +00:00
|
|
|
let _usize_from_fn_ptr_transmute = unsafe { transmute::<fn(usize) -> u8, usize>(foo) };
|
2020-08-09 06:28:56 +00:00
|
|
|
let _usize_from_fn_ptr = foo as *const usize;
|
2023-01-12 18:00:03 +00:00
|
|
|
|
|
|
|
let _usize_from_ref = unsafe { transmute::<*const u32, usize>(&1u32) };
|
2020-08-03 00:41:50 +00:00
|
|
|
}
|
2020-08-06 02:23:29 +00:00
|
|
|
|
|
|
|
// If a ref-to-ptr cast of this form where the pointer type points to a type other
|
|
|
|
// than the referenced type, calling `CastCheck::do_check` has been observed to
|
|
|
|
// cause an ICE error message. `do_check` is currently called inside the
|
|
|
|
// `transmutes_expressible_as_ptr_casts` check, but other, more specific lints
|
|
|
|
// currently prevent it from being called in these cases. This test is meant to
|
|
|
|
// fail if the ordering of the checks ever changes enough to cause these cases to
|
|
|
|
// fall through into `do_check`.
|
|
|
|
fn trigger_do_check_to_emit_error(in_param: &[i32; 1]) -> *const u8 {
|
|
|
|
unsafe { transmute::<&[i32; 1], *const u8>(in_param) }
|
2020-08-06 10:49:06 +00:00
|
|
|
}
|
2020-08-09 06:15:56 +00:00
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
struct Single(u64);
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
struct Pair(u32, u32);
|
|
|
|
|
|
|
|
fn cannot_be_expressed_as_pointer_cast(in_param: Single) -> Pair {
|
|
|
|
assert_eq!(size_of::<Single>(), size_of::<Pair>());
|
|
|
|
|
|
|
|
unsafe { transmute::<Single, Pair>(in_param) }
|
|
|
|
}
|
2023-03-23 18:58:10 +00:00
|
|
|
|
|
|
|
fn issue_10449() {
|
|
|
|
fn f() {}
|
|
|
|
|
|
|
|
let _x: u8 = unsafe { *std::mem::transmute::<fn(), *const u8>(f) };
|
|
|
|
}
|
2024-03-03 09:32:39 +00:00
|
|
|
|
|
|
|
// 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) };
|
|
|
|
}
|