]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/ptr_as_ptr.rs
Merge commit '370c397ec9169809e5ad270079712e0043514240' into sync_cg_clif-2022-03-20
[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 #![feature(custom_inner_attributes)]
6
7 extern crate macro_rules;
8
9 macro_rules! cast_it {
10     ($ptr: ident) => {
11         $ptr as *const i32
12     };
13 }
14
15 fn main() {
16     let ptr: *const u32 = &42_u32;
17     let mut_ptr: *mut u32 = &mut 42_u32;
18
19     let _ = ptr as *const i32;
20     let _ = mut_ptr as *mut i32;
21
22     // Make sure the lint can handle the difference in their operator precedences.
23     unsafe {
24         let ptr_ptr: *const *const u32 = &ptr;
25         let _ = *ptr_ptr as *const i32;
26     }
27
28     // Changes in mutability. Do not lint this.
29     let _ = ptr as *mut i32;
30     let _ = mut_ptr as *const i32;
31
32     // `pointer::cast` cannot perform unsized coercions unlike `as`. Do not lint this.
33     let ptr_of_array: *const [u32; 4] = &[1, 2, 3, 4];
34     let _ = ptr_of_array as *const [u32];
35     let _ = ptr_of_array as *const dyn std::fmt::Debug;
36
37     // Ensure the lint doesn't produce unnecessary turbofish for inferred types.
38     let _: *const i32 = ptr as *const _;
39     let _: *mut i32 = mut_ptr as _;
40
41     // Make sure the lint is triggered inside a macro
42     let _ = cast_it!(ptr);
43
44     // Do not lint inside macros from external crates
45     let _ = macro_rules::ptr_as_ptr_cast!(ptr);
46 }
47
48 fn _msrv_1_37() {
49     #![clippy::msrv = "1.37"]
50     let ptr: *const u32 = &42_u32;
51     let mut_ptr: *mut u32 = &mut 42_u32;
52
53     // `pointer::cast` was stabilized in 1.38. Do not lint this
54     let _ = ptr as *const i32;
55     let _ = mut_ptr as *mut i32;
56 }
57
58 fn _msrv_1_38() {
59     #![clippy::msrv = "1.38"]
60     let ptr: *const u32 = &42_u32;
61     let mut_ptr: *mut u32 = &mut 42_u32;
62
63     let _ = ptr as *const i32;
64     let _ = mut_ptr as *mut i32;
65 }