]> git.lizzy.rs Git - rust.git/blob - src/docs/swap_ptr_to_ref.txt
[Arithmetic] Consider literals
[rust.git] / src / docs / swap_ptr_to_ref.txt
1 ### What it does
2 Checks for calls to `core::mem::swap` where either parameter is derived from a pointer
3
4 ### Why is this bad?
5 When at least one parameter to `swap` is derived from a pointer it may overlap with the
6 other. This would then lead to undefined behavior.
7
8 ### Example
9 ```
10 unsafe fn swap(x: &[*mut u32], y: &[*mut u32]) {
11     for (&x, &y) in x.iter().zip(y) {
12         core::mem::swap(&mut *x, &mut *y);
13     }
14 }
15 ```
16 Use instead:
17 ```
18 unsafe fn swap(x: &[*mut u32], y: &[*mut u32]) {
19     for (&x, &y) in x.iter().zip(y) {
20         core::ptr::swap(x, y);
21     }
22 }
23 ```