]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/src/docs/cast_ptr_alignment.txt
Auto merge of #104673 - matthiaskrgr:rollup-85f65ov, r=matthiaskrgr
[rust.git] / src / tools / clippy / src / docs / cast_ptr_alignment.txt
1 ### What it does
2 Checks for casts, using `as` or `pointer::cast`,
3 from a less-strictly-aligned pointer to a more-strictly-aligned pointer
4
5 ### Why is this bad?
6 Dereferencing the resulting pointer may be undefined
7 behavior.
8
9 ### Known problems
10 Using `std::ptr::read_unaligned` and `std::ptr::write_unaligned` or similar
11 on the resulting pointer is fine. Is over-zealous: Casts with manual alignment checks or casts like
12 u64-> u8 -> u16 can be fine. Miri is able to do a more in-depth analysis.
13
14 ### Example
15 ```
16 let _ = (&1u8 as *const u8) as *const u16;
17 let _ = (&mut 1u8 as *mut u8) as *mut u16;
18
19 (&1u8 as *const u8).cast::<u16>();
20 (&mut 1u8 as *mut u8).cast::<u16>();
21 ```