|
| 1 | +// run-rustfix |
| 2 | +#![warn(clippy::transmutes_expressible_as_ptr_casts)] |
| 3 | +// These two warnings currrently 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 | + |
| 8 | +use std::mem::transmute; |
| 9 | + |
| 10 | +// rustc_typeck::check::cast contains documentation about when a cast `e as U` is |
| 11 | +// valid, which we quote from below. |
| 12 | + |
| 13 | +fn main() { |
| 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. |
| 16 | + |
| 17 | + // e is an integer and U is *U_0, while U_0: Sized; addr-ptr-cast |
| 18 | + let ptr_i32_transmute = unsafe { |
| 19 | + -1 as *const i32 |
| 20 | + }; |
| 21 | + let ptr_i32 = -1isize as *const i32; |
| 22 | + |
| 23 | + // e has type *T, U is *U_0, and either U_0: Sized ... |
| 24 | + let ptr_i8_transmute = unsafe { |
| 25 | + ptr_i32 as *const i8 |
| 26 | + }; |
| 27 | + let ptr_i8 = ptr_i32 as *const i8; |
| 28 | + |
| 29 | + let slice_ptr = &[0,1,2,3] as *const [i32]; |
| 30 | + |
| 31 | + // ... or pointer_kind(T) = pointer_kind(U_0); ptr-ptr-cast |
| 32 | + let ptr_to_unsized_transmute = unsafe { |
| 33 | + slice_ptr as *const [u16] |
| 34 | + }; |
| 35 | + let ptr_to_unsized = slice_ptr as *const [u16]; |
| 36 | + // TODO: We could try testing vtable casts here too, but maybe |
| 37 | + // we should wait until std::raw::TraitObject is stabilized? |
| 38 | + |
| 39 | + // e has type *T and U is a numeric type, while T: Sized; ptr-addr-cast |
| 40 | + let usize_from_int_ptr_transmute = unsafe { |
| 41 | + ptr_i32 as usize |
| 42 | + }; |
| 43 | + let usize_from_int_ptr = ptr_i32 as usize; |
| 44 | + |
| 45 | + let array_ref: &[i32; 4] = &[1,2,3,4]; |
| 46 | + |
| 47 | + // e has type &[T; n] and U is *const T; array-ptr-cast |
| 48 | + let array_ptr_transmute = unsafe { |
| 49 | + array_ref as *const [i32; 4] |
| 50 | + }; |
| 51 | + let array_ptr = array_ref as *const [i32; 4]; |
| 52 | + |
| 53 | + fn foo(_: usize) -> u8 { 42 } |
| 54 | + |
| 55 | + // e is a function pointer type and U has type *T, while T: Sized; fptr-ptr-cast |
| 56 | + let usize_ptr_transmute = unsafe { |
| 57 | + foo as *const usize |
| 58 | + }; |
| 59 | + let usize_ptr_transmute = foo as *const usize; |
| 60 | + |
| 61 | + // e is a function pointer type and U is an integer; fptr-addr-cast |
| 62 | + let usize_from_fn_ptr_transmute = unsafe { |
| 63 | + foo as usize |
| 64 | + }; |
| 65 | + let usize_from_fn_ptr = foo as *const usize; |
| 66 | +} |
| 67 | + |
| 68 | +// If a ref-to-ptr cast of this form where the pointer type points to a type other |
| 69 | +// than the referenced type, calling `CastCheck::do_check` has been observed to |
| 70 | +// cause an ICE error message. `do_check` is currently called inside the |
| 71 | +// `transmutes_expressible_as_ptr_casts` check, but other, more specific lints |
| 72 | +// currently prevent it from being called in these cases. This test is meant to |
| 73 | +// fail if the ordering of the checks ever changes enough to cause these cases to |
| 74 | +// fall through into `do_check`. |
| 75 | +fn trigger_do_check_to_emit_error(in_param: &[i32; 1]) -> *const u8 { |
| 76 | + unsafe { in_param as *const [i32; 1] as *const u8 } |
| 77 | +} |
0 commit comments