]> git.lizzy.rs Git - rust.git/blob - src/docs/ptr_as_ptr.txt
Add iter_kv_map lint
[rust.git] / src / docs / ptr_as_ptr.txt
1 ### What it does
2 Checks for `as` casts between raw pointers without changing its mutability,
3 namely `*const T` to `*const U` and `*mut T` to `*mut U`.
4
5 ### Why is this bad?
6 Though `as` casts between raw pointers is not terrible, `pointer::cast` is safer because
7 it cannot accidentally change the pointer's mutability nor cast the pointer to other types like `usize`.
8
9 ### Example
10 ```
11 let ptr: *const u32 = &42_u32;
12 let mut_ptr: *mut u32 = &mut 42_u32;
13 let _ = ptr as *const i32;
14 let _ = mut_ptr as *mut i32;
15 ```
16 Use instead:
17 ```
18 let ptr: *const u32 = &42_u32;
19 let mut_ptr: *mut u32 = &mut 42_u32;
20 let _ = ptr.cast::<i32>();
21 let _ = mut_ptr.cast::<i32>();
22 ```