rust-clippy/tests/ui/ptr_cast_constness.fixed

56 lines
1.4 KiB
Rust
Raw Normal View History

2023-05-15 00:25:23 +00:00
//@run-rustfix
//@aux-build:proc_macros.rs
#![warn(clippy::ptr_cast_constness)]
extern crate proc_macros;
use proc_macros::{external, inline_macros};
#[inline_macros]
fn main() {
let ptr: *const u32 = &42_u32;
let mut_ptr: *mut u32 = &mut 42_u32;
let _ = ptr as *const i32;
let _ = mut_ptr as *mut i32;
// 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
let _ = inline!($ptr as *const i32);
// Do not lint inside macros from external crates
let _ = external!($ptr as *const i32);
}
#[clippy::msrv = "1.64"]
2023-05-16 16:20:00 +00:00
fn _msrv_1_64() {
2023-05-15 00:25:23 +00: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-05-15 00:31:12 +00:00
let _ = ptr as *mut i32;
let _ = mut_ptr as *const i32;
2023-05-15 00:25:23 +00:00
}
#[clippy::msrv = "1.65"]
2023-05-16 16:20:00 +00:00
fn _msrv_1_65() {
2023-05-15 00:25:23 +00:00
let ptr: *const u32 = &42_u32;
let mut_ptr: *mut u32 = &mut 42_u32;
let _ = ptr.cast_mut();
let _ = mut_ptr.cast_const();
}