RS.CLIPPY.SWAP_PTR_TO_REF

Call to `mem::swap` using pointer derived references

This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: swap_ptr_to_ref. Copyright ©2025 The Rust Team. All rights reserved.

What it does

Checks for calls to core::mem::swap where either parameter is derived from a pointer

Why is this bad?

When at least one parameter to swap is derived from a pointer it may overlap with the other. This would then lead to undefined behavior.

Example

unsafe fn swap(x: &[*mut u32], y: &[*mut u32]) {
    for (&x, &y) in x.iter().zip(y) {
        core::mem::swap(&mut *x, &mut *y);
    }
}

Use instead:

unsafe fn swap(x: &[*mut u32], y: &[*mut u32]) {
    for (&x, &y) in x.iter().zip(y) {
        core::ptr::swap(x, y);
    }
}