2 #![warn(clippy::transmutes_expressible_as_ptr_casts)]
3 // These two warnings currently cover the cases transmutes_expressible_as_ptr_casts
4 // would otherwise be responsible for
5 #![warn(clippy::useless_transmute)]
6 #![warn(clippy::transmute_ptr_to_ptr)]
7 #![allow(dead_code, unused_unsafe, clippy::borrow_as_ptr)]
9 use std::mem::{size_of, transmute};
11 // rustc_typeck::check::cast contains documentation about when a cast `e as U` is
12 // valid, which we quote from below.
14 // We should see an error message for each transmute, and no error messages for
15 // the casts, since the casts are the recommended fixes.
17 // e is an integer and U is *U_0, while U_0: Sized; addr-ptr-cast
18 let _ptr_i32_transmute = unsafe { transmute::<usize, *const i32>(usize::MAX) };
19 let ptr_i32 = usize::MAX as *const i32;
21 // e has type *T, U is *U_0, and either U_0: Sized ...
22 let _ptr_i8_transmute = unsafe { transmute::<*const i32, *const i8>(ptr_i32) };
23 let _ptr_i8 = ptr_i32 as *const i8;
25 let slice_ptr = &[0, 1, 2, 3] as *const [i32];
27 // ... or pointer_kind(T) = pointer_kind(U_0); ptr-ptr-cast
28 let _ptr_to_unsized_transmute = unsafe { transmute::<*const [i32], *const [u32]>(slice_ptr) };
29 let _ptr_to_unsized = slice_ptr as *const [u32];
30 // TODO: We could try testing vtable casts here too, but maybe
31 // we should wait until std::raw::TraitObject is stabilized?
33 // e has type *T and U is a numeric type, while T: Sized; ptr-addr-cast
34 let _usize_from_int_ptr_transmute = unsafe { transmute::<*const i32, usize>(ptr_i32) };
35 let _usize_from_int_ptr = ptr_i32 as usize;
37 let array_ref: &[i32; 4] = &[1, 2, 3, 4];
39 // e has type &[T; n] and U is *const T; array-ptr-cast
40 let _array_ptr_transmute = unsafe { transmute::<&[i32; 4], *const [i32; 4]>(array_ref) };
41 let _array_ptr = array_ref as *const [i32; 4];
43 fn foo(_: usize) -> u8 {
47 // e is a function pointer type and U has type *T, while T: Sized; fptr-ptr-cast
48 let _usize_ptr_transmute = unsafe { transmute::<fn(usize) -> u8, *const usize>(foo) };
49 let _usize_ptr_transmute = foo as *const usize;
51 // e is a function pointer type and U is an integer; fptr-addr-cast
52 let _usize_from_fn_ptr_transmute = unsafe { transmute::<fn(usize) -> u8, usize>(foo) };
53 let _usize_from_fn_ptr = foo as *const usize;
56 // If a ref-to-ptr cast of this form where the pointer type points to a type other
57 // than the referenced type, calling `CastCheck::do_check` has been observed to
58 // cause an ICE error message. `do_check` is currently called inside the
59 // `transmutes_expressible_as_ptr_casts` check, but other, more specific lints
60 // currently prevent it from being called in these cases. This test is meant to
61 // fail if the ordering of the checks ever changes enough to cause these cases to
62 // fall through into `do_check`.
63 fn trigger_do_check_to_emit_error(in_param: &[i32; 1]) -> *const u8 {
64 unsafe { transmute::<&[i32; 1], *const u8>(in_param) }
71 struct Pair(u32, u32);
73 fn cannot_be_expressed_as_pointer_cast(in_param: Single) -> Pair {
74 assert_eq!(size_of::<Single>(), size_of::<Pair>());
76 unsafe { transmute::<Single, Pair>(in_param) }