mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-24 13:43:17 +00:00
106 lines
3.1 KiB
Rust
106 lines
3.1 KiB
Rust
#![warn(clippy::transmute_ptr_to_ptr)]
|
|
#![allow(clippy::borrow_as_ptr, clippy::missing_transmute_annotations)]
|
|
|
|
use std::mem::transmute;
|
|
|
|
// 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 {
|
|
transmute::<&'a T, &'static T>(t)
|
|
}
|
|
|
|
// Make sure we can do non-static lifetime transmutes
|
|
unsafe fn transmute_lifetime<'a, 'b, T>(t: &'a T, u: &'b T) -> &'b T {
|
|
transmute::<&'a T, &'b T>(t)
|
|
}
|
|
|
|
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
|
|
let _: *const f32 = ptr.cast::<f32>();
|
|
//~^ transmute_ptr_to_ptr
|
|
let _: *mut f32 = mut_ptr.cast::<f32>();
|
|
//~^ transmute_ptr_to_ptr
|
|
// ref-ref transmutes; bad
|
|
let _: &f32 = &*(&1u32 as *const u32 as *const f32);
|
|
//~^ transmute_ptr_to_ptr
|
|
let _: &f32 = &*(&1f64 as *const f64 as *const f32);
|
|
//~^ transmute_ptr_to_ptr
|
|
//:^ 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);
|
|
//~^ transmute_ptr_to_ptr
|
|
let _: &GenericParam<f32> = &*(&GenericParam { t: 1u32 } as *const GenericParam<u32> as *const GenericParam<f32>);
|
|
//~^ transmute_ptr_to_ptr
|
|
let u64_ref: &u64 = &0u64;
|
|
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
|
|
}
|
|
|
|
// transmute internal lifetimes, should not lint
|
|
let s = "hello world".to_owned();
|
|
let lp = LifetimeParam { s: &s };
|
|
let _: &LifetimeParam<'static> = unsafe { transmute(&lp) };
|
|
let _: &GenericParam<&LifetimeParam<'static>> = unsafe { transmute(&GenericParam { t: &lp }) };
|
|
}
|
|
|
|
fn lifetime_to_static(v: *mut &()) -> *const &'static () {
|
|
unsafe { v as *const &() }
|
|
//~^ transmute_ptr_to_ptr
|
|
}
|
|
|
|
// dereferencing raw pointers in const contexts, should not lint as it's unstable (issue 5959)
|
|
const _: &() = {
|
|
struct Zst;
|
|
let zst = &Zst;
|
|
|
|
unsafe { transmute::<&'static Zst, &'static ()>(zst) }
|
|
};
|
|
|
|
#[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();
|
|
}
|
|
}
|
|
|
|
fn main() {}
|