2023-07-27 08:23:04 -05:00
|
|
|
//@aux-build:proc_macros.rs
|
2023-05-14 19:25:23 -05:00
|
|
|
|
|
|
|
#![warn(clippy::ptr_cast_constness)]
|
2024-02-19 07:53:53 -06:00
|
|
|
#![allow(
|
|
|
|
clippy::transmute_ptr_to_ref,
|
|
|
|
clippy::unnecessary_cast,
|
|
|
|
unused,
|
|
|
|
clippy::missing_transmute_annotations
|
|
|
|
)]
|
2023-05-14 19:25:23 -05:00
|
|
|
|
|
|
|
extern crate proc_macros;
|
|
|
|
use proc_macros::{external, inline_macros};
|
|
|
|
|
2023-06-02 13:14:16 -05:00
|
|
|
unsafe fn ptr_to_ref<T, U>(p: *const T, om: *mut U) {
|
|
|
|
let _: &mut T = std::mem::transmute(p.cast_mut());
|
|
|
|
let _ = &mut *p.cast_mut();
|
|
|
|
let _: &T = &*(om as *const T);
|
|
|
|
}
|
|
|
|
|
2023-05-14 19:25:23 -05:00
|
|
|
#[inline_macros]
|
|
|
|
fn main() {
|
|
|
|
let ptr: *const u32 = &42_u32;
|
|
|
|
let mut_ptr: *mut u32 = &mut 42_u32;
|
|
|
|
|
2023-06-02 13:14:16 -05:00
|
|
|
let _ = ptr as *const u32;
|
|
|
|
let _ = mut_ptr as *mut u32;
|
2023-05-14 19:25:23 -05:00
|
|
|
|
|
|
|
// Make sure the lint can handle the difference in their operator precedences.
|
|
|
|
unsafe {
|
|
|
|
let ptr_ptr: *const *const u32 = &ptr;
|
|
|
|
let _ = (*ptr_ptr).cast_mut();
|
|
|
|
}
|
|
|
|
|
|
|
|
let _ = ptr.cast_mut();
|
|
|
|
let _ = mut_ptr.cast_const();
|
|
|
|
|
|
|
|
// Lint this, since pointer::cast_mut and pointer::cast_const have ?Sized
|
|
|
|
let ptr_of_array: *const [u32; 4] = &[1, 2, 3, 4];
|
|
|
|
let _ = ptr_of_array as *const [u32];
|
|
|
|
let _ = ptr_of_array as *const dyn std::fmt::Debug;
|
|
|
|
|
|
|
|
// Make sure the lint is triggered inside a macro
|
2023-06-02 13:14:16 -05:00
|
|
|
let _ = inline!($ptr as *const u32);
|
2023-05-14 19:25:23 -05:00
|
|
|
|
|
|
|
// Do not lint inside macros from external crates
|
2023-06-02 13:14:16 -05:00
|
|
|
let _ = external!($ptr as *const u32);
|
2023-05-14 19:25:23 -05:00
|
|
|
}
|
|
|
|
|
2024-07-23 06:11:22 -05:00
|
|
|
fn lifetime_to_static(v: *mut &()) -> *const &'static () {
|
|
|
|
v as _
|
|
|
|
}
|
|
|
|
|
2023-05-14 19:25:23 -05:00
|
|
|
#[clippy::msrv = "1.64"]
|
2023-05-16 11:20:00 -05:00
|
|
|
fn _msrv_1_64() {
|
2023-05-14 19:25:23 -05:00
|
|
|
let ptr: *const u32 = &42_u32;
|
|
|
|
let mut_ptr: *mut u32 = &mut 42_u32;
|
|
|
|
|
|
|
|
// `pointer::cast_const` and `pointer::cast_mut` were stabilized in 1.65. Do not lint this
|
2023-06-02 13:14:16 -05:00
|
|
|
let _ = ptr as *mut u32;
|
|
|
|
let _ = mut_ptr as *const u32;
|
2023-05-14 19:25:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[clippy::msrv = "1.65"]
|
2023-05-16 11:20:00 -05:00
|
|
|
fn _msrv_1_65() {
|
2023-05-14 19:25:23 -05:00
|
|
|
let ptr: *const u32 = &42_u32;
|
|
|
|
let mut_ptr: *mut u32 = &mut 42_u32;
|
|
|
|
|
|
|
|
let _ = ptr.cast_mut();
|
|
|
|
let _ = mut_ptr.cast_const();
|
|
|
|
}
|
2024-09-07 09:18:45 -05:00
|
|
|
|
|
|
|
#[inline_macros]
|
|
|
|
fn null_pointers() {
|
|
|
|
use std::ptr;
|
|
|
|
let _ = std::ptr::null_mut::<String>();
|
|
|
|
let _ = std::ptr::null::<u32>();
|
2024-09-07 14:57:29 -05:00
|
|
|
let _ = std::ptr::null_mut::<u32>();
|
|
|
|
let _ = std::ptr::null::<u32>();
|
2024-09-07 09:18:45 -05:00
|
|
|
|
|
|
|
// Make sure the lint is triggered inside a macro
|
|
|
|
let _ = inline!(std::ptr::null_mut::<u32>());
|
2024-09-07 14:57:29 -05:00
|
|
|
let _ = inline!(std::ptr::null_mut::<u32>());
|
2024-09-07 09:18:45 -05:00
|
|
|
|
|
|
|
// Do not lint inside macros from external crates
|
|
|
|
let _ = external!(ptr::null::<u32>() as *mut u32);
|
2024-09-07 14:57:29 -05:00
|
|
|
let _ = external!(ptr::null::<u32>().cast_mut());
|
2024-09-07 09:18:45 -05:00
|
|
|
}
|