2023-08-24 14:32:12 -05:00
|
|
|
#![warn(clippy::transmute_ptr_to_ptr)]
|
2024-02-19 07:53:53 -06:00
|
|
|
#![allow(clippy::borrow_as_ptr, clippy::missing_transmute_annotations)]
|
2023-08-24 14:32:12 -05:00
|
|
|
|
2024-07-22 12:20:23 -05:00
|
|
|
use std::mem::transmute;
|
|
|
|
|
2023-08-24 14:32:12 -05:00
|
|
|
// Make sure we can modify lifetimes, which is one of the recommended uses
|
|
|
|
// of transmute
|
|
|
|
|
|
|
|
// Make sure we can do static lifetime transmutes
|
|
|
|
unsafe fn transmute_lifetime_to_static<'a, T>(t: &'a T) -> &'static T {
|
2024-07-22 12:20:23 -05:00
|
|
|
transmute::<&'a T, &'static T>(t)
|
2023-08-24 14:32:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we can do non-static lifetime transmutes
|
|
|
|
unsafe fn transmute_lifetime<'a, 'b, T>(t: &'a T, u: &'b T) -> &'b T {
|
2024-07-22 12:20:23 -05:00
|
|
|
transmute::<&'a T, &'b T>(t)
|
2023-08-24 14:32:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
struct LifetimeParam<'a> {
|
|
|
|
s: &'a str,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct GenericParam<T> {
|
|
|
|
t: T,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn transmute_ptr_to_ptr() {
|
|
|
|
let ptr = &1u32 as *const u32;
|
|
|
|
let mut_ptr = &mut 1u32 as *mut u32;
|
|
|
|
unsafe {
|
|
|
|
// pointer-to-pointer transmutes; bad
|
2024-07-22 12:20:23 -05:00
|
|
|
let _: *const f32 = ptr.cast::<f32>();
|
|
|
|
//~^ transmute_ptr_to_ptr
|
|
|
|
let _: *mut f32 = mut_ptr.cast::<f32>();
|
|
|
|
//~^ transmute_ptr_to_ptr
|
2023-08-24 14:32:12 -05:00
|
|
|
// ref-ref transmutes; bad
|
|
|
|
let _: &f32 = &*(&1u32 as *const u32 as *const f32);
|
2024-07-22 12:20:23 -05:00
|
|
|
//~^ transmute_ptr_to_ptr
|
2023-12-17 10:28:24 -06:00
|
|
|
let _: &f32 = &*(&1f64 as *const f64 as *const f32);
|
2024-07-22 12:20:23 -05:00
|
|
|
//~^ transmute_ptr_to_ptr
|
2023-08-24 14:32:12 -05:00
|
|
|
//:^ this test is here because both f32 and f64 are the same TypeVariant, but they are not
|
|
|
|
// the same type
|
|
|
|
let _: &mut f32 = &mut *(&mut 1u32 as *mut u32 as *mut f32);
|
2024-07-22 12:20:23 -05:00
|
|
|
//~^ transmute_ptr_to_ptr
|
2023-08-24 14:32:12 -05:00
|
|
|
let _: &GenericParam<f32> = &*(&GenericParam { t: 1u32 } as *const GenericParam<u32> as *const GenericParam<f32>);
|
2024-07-22 12:20:23 -05:00
|
|
|
//~^ transmute_ptr_to_ptr
|
2023-12-17 10:28:24 -06:00
|
|
|
let u64_ref: &u64 = &0u64;
|
2024-07-22 12:20:23 -05:00
|
|
|
let u8_ref: &u8 = &*(u64_ref as *const u64 as *const u8);
|
|
|
|
//~^ transmute_ptr_to_ptr
|
|
|
|
let _: *const u32 = mut_ptr.cast_const();
|
|
|
|
//~^ transmute_ptr_to_ptr
|
|
|
|
let _: *mut u32 = ptr.cast_mut();
|
|
|
|
//~^ transmute_ptr_to_ptr
|
2023-08-24 14:32:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// transmute internal lifetimes, should not lint
|
|
|
|
let s = "hello world".to_owned();
|
|
|
|
let lp = LifetimeParam { s: &s };
|
2024-07-22 12:20:23 -05:00
|
|
|
let _: &LifetimeParam<'static> = unsafe { transmute(&lp) };
|
|
|
|
let _: &GenericParam<&LifetimeParam<'static>> = unsafe { transmute(&GenericParam { t: &lp }) };
|
2023-08-24 14:32:12 -05:00
|
|
|
}
|
|
|
|
|
2024-07-23 06:11:22 -05:00
|
|
|
fn lifetime_to_static(v: *mut &()) -> *const &'static () {
|
|
|
|
unsafe { v as *const &() }
|
|
|
|
//~^ transmute_ptr_to_ptr
|
|
|
|
}
|
|
|
|
|
2023-08-24 14:32:12 -05:00
|
|
|
// dereferencing raw pointers in const contexts, should not lint as it's unstable (issue 5959)
|
|
|
|
const _: &() = {
|
|
|
|
struct Zst;
|
|
|
|
let zst = &Zst;
|
|
|
|
|
2024-07-22 12:20:23 -05:00
|
|
|
unsafe { transmute::<&'static Zst, &'static ()>(zst) }
|
2023-08-24 14:32:12 -05:00
|
|
|
};
|
|
|
|
|
2024-07-22 12:20:23 -05:00
|
|
|
#[clippy::msrv = "1.37"]
|
|
|
|
fn msrv_1_37(ptr: *const u8) {
|
|
|
|
unsafe {
|
|
|
|
let _: *const i8 = ptr as *const i8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[clippy::msrv = "1.38"]
|
|
|
|
fn msrv_1_38(ptr: *const u8) {
|
|
|
|
unsafe {
|
|
|
|
let _: *const i8 = ptr.cast::<i8>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[clippy::msrv = "1.64"]
|
|
|
|
fn msrv_1_64(ptr: *const u8, mut_ptr: *mut u8) {
|
|
|
|
unsafe {
|
|
|
|
let _: *mut u8 = ptr as *mut u8;
|
|
|
|
let _: *const u8 = mut_ptr as *const u8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[clippy::msrv = "1.65"]
|
|
|
|
fn msrv_1_65(ptr: *const u8, mut_ptr: *mut u8) {
|
|
|
|
unsafe {
|
|
|
|
let _: *mut u8 = ptr.cast_mut();
|
|
|
|
let _: *const u8 = mut_ptr.cast_const();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-24 14:32:12 -05:00
|
|
|
fn main() {}
|