]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/ptr_as_ptr.rs
Auto merge of #107843 - bjorn3:sync_cg_clif-2023-02-09, r=bjorn3
[rust.git] / src / tools / clippy / tests / ui / ptr_as_ptr.rs
1 // run-rustfix
2 // aux-build:macro_rules.rs
3
4 #![warn(clippy::ptr_as_ptr)]
5
6 extern crate macro_rules;
7
8 macro_rules! cast_it {
9     ($ptr: ident) => {
10         $ptr as *const i32
11     };
12 }
13
14 fn main() {
15     let ptr: *const u32 = &42_u32;
16     let mut_ptr: *mut u32 = &mut 42_u32;
17
18     let _ = ptr as *const i32;
19     let _ = mut_ptr as *mut i32;
20
21     // Make sure the lint can handle the difference in their operator precedences.
22     unsafe {
23         let ptr_ptr: *const *const u32 = &ptr;
24         let _ = *ptr_ptr as *const i32;
25     }
26
27     // Changes in mutability. Do not lint this.
28     let _ = ptr as *mut i32;
29     let _ = mut_ptr as *const i32;
30
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;
35
36     // Ensure the lint doesn't produce unnecessary turbofish for inferred types.
37     let _: *const i32 = ptr as *const _;
38     let _: *mut i32 = mut_ptr as _;
39
40     // Make sure the lint is triggered inside a macro
41     let _ = cast_it!(ptr);
42
43     // Do not lint inside macros from external crates
44     let _ = macro_rules::ptr_as_ptr_cast!(ptr);
45 }
46
47 #[clippy::msrv = "1.37"]
48 fn _msrv_1_37() {
49     let ptr: *const u32 = &42_u32;
50     let mut_ptr: *mut u32 = &mut 42_u32;
51
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;
55 }
56
57 #[clippy::msrv = "1.38"]
58 fn _msrv_1_38() {
59     let ptr: *const u32 = &42_u32;
60     let mut_ptr: *mut u32 = &mut 42_u32;
61
62     let _ = ptr as *const i32;
63     let _ = mut_ptr as *mut i32;
64 }