rust/tests/ui/ptr_as_ptr.rs
rail 4b478a5731 Add a new lint ptr_as_ptr,
which checks for `as` casts between raw pointers
without changing its mutability
and suggest replacing it with `pointer::cast`.
2021-01-05 10:19:03 +13:00

31 lines
904 B
Rust

// run-rustfix
#![warn(clippy::ptr_as_ptr)]
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 as *const i32;
}
// Changes in mutability. Do not lint this.
let _ = ptr as *mut i32;
let _ = mut_ptr as *const i32;
// `pointer::cast` cannot perform unsized coercions unlike `as`. Do not lint this.
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;
// Ensure the lint doesn't produce unnecessary turbofish for inferred types.
let _: *const i32 = ptr as *const _;
let _: *mut i32 = mut_ptr as _;
}