2022-12-17 11:22:52 -06:00
|
|
|
#![allow(dead_code)]
|
|
|
|
#![warn(clippy::transmute_null_to_fn)]
|
|
|
|
#![allow(clippy::zero_ptr)]
|
|
|
|
|
|
|
|
// Easy to lint because these only span one line.
|
|
|
|
fn one_liners() {
|
|
|
|
unsafe {
|
|
|
|
let _: fn() = std::mem::transmute(0 as *const ());
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: transmuting a known null pointer into a function pointer
|
2022-12-17 11:22:52 -06:00
|
|
|
let _: fn() = std::mem::transmute(std::ptr::null::<()>());
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: transmuting a known null pointer into a function pointer
|
2022-12-17 11:22:52 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub const ZPTR: *const usize = 0 as *const _;
|
|
|
|
pub const NOT_ZPTR: *const usize = 1 as *const _;
|
|
|
|
|
|
|
|
fn transmute_const() {
|
|
|
|
unsafe {
|
|
|
|
// Should raise a lint.
|
|
|
|
let _: fn() = std::mem::transmute(ZPTR);
|
2023-07-28 14:35:48 -05:00
|
|
|
//~^ ERROR: transmuting a known null pointer into a function pointer
|
2022-12-17 11:22:52 -06:00
|
|
|
// Should NOT raise a lint.
|
|
|
|
let _: fn() = std::mem::transmute(NOT_ZPTR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-14 23:24:45 -05:00
|
|
|
fn issue_11485() {
|
|
|
|
unsafe {
|
|
|
|
let _: fn() = std::mem::transmute(0 as *const u8 as *const ());
|
|
|
|
//~^ ERROR: transmuting a known null pointer into a function pointer
|
|
|
|
let _: fn() = std::mem::transmute(std::ptr::null::<()>() as *const u8);
|
|
|
|
//~^ ERROR: transmuting a known null pointer into a function pointer
|
|
|
|
let _: fn() = std::mem::transmute(ZPTR as *const u8);
|
|
|
|
//~^ ERROR: transmuting a known null pointer into a function pointer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-17 11:22:52 -06:00
|
|
|
fn main() {
|
|
|
|
one_liners();
|
|
|
|
transmute_const();
|
|
|
|
}
|