2 // aux-build:macro_rules.rs
4 #![warn(clippy::ptr_as_ptr)]
6 extern crate macro_rules;
15 let ptr: *const u32 = &42_u32;
16 let mut_ptr: *mut u32 = &mut 42_u32;
18 let _ = ptr.cast::<i32>();
19 let _ = mut_ptr.cast::<i32>();
21 // Make sure the lint can handle the difference in their operator precedences.
23 let ptr_ptr: *const *const u32 = &ptr;
24 let _ = (*ptr_ptr).cast::<i32>();
27 // Changes in mutability. Do not lint this.
28 let _ = ptr as *mut i32;
29 let _ = mut_ptr as *const i32;
31 // `pointer::cast` cannot perform unsized coercions unlike `as`. Do not lint this.
32 let ptr_of_array: *const [u32; 4] = &[1, 2, 3, 4];
33 let _ = ptr_of_array as *const [u32];
34 let _ = ptr_of_array as *const dyn std::fmt::Debug;
36 // Ensure the lint doesn't produce unnecessary turbofish for inferred types.
37 let _: *const i32 = ptr.cast();
38 let _: *mut i32 = mut_ptr.cast();
40 // Make sure the lint is triggered inside a macro
41 let _ = cast_it!(ptr);
43 // Do not lint inside macros from external crates
44 let _ = macro_rules::ptr_as_ptr_cast!(ptr);
47 #[clippy::msrv = "1.37"]
49 let ptr: *const u32 = &42_u32;
50 let mut_ptr: *mut u32 = &mut 42_u32;
52 // `pointer::cast` was stabilized in 1.38. Do not lint this
53 let _ = ptr as *const i32;
54 let _ = mut_ptr as *mut i32;
57 #[clippy::msrv = "1.38"]
59 let ptr: *const u32 = &42_u32;
60 let mut_ptr: *mut u32 = &mut 42_u32;
62 let _ = ptr.cast::<i32>();
63 let _ = mut_ptr.cast::<i32>();