Email me

Clippy lints / Suspicious

Clippy lint · Suspicious · checked with Clippy 0.1.99

clippy::swap_ptr_to_ref: call to `core::mem::swap` with a parameter derived from a raw pointer

What it meansChecks for calls to core::mem::swap where either parameter is derived from a pointer It is in the suspicious group and warns by default.

What you see

warning: call to `core::mem::swap` with a parameter derived from a raw pointer
 --> main.rs:5:9
  |
5 |         core::mem::swap(&mut *x, &mut *y);
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use ptr::swap: `core::ptr::swap(x, y)`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#swap_ptr_to_ref
  = note: `#[warn(clippy::swap_ptr_to_ref)]` on by default

Real output of Clippy 0.1.99 (Rust 1.99) on the example below. Names and line numbers in yours will differ.

Does it matter?

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.

Code that is most likely wrong or useless. Warns by default.

How to fix it

Clippy can fix this one for you: run cargo clippy --fix (add --allow-dirty if you have uncommitted changes). Review the diff before you commit.

Flagged

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

Write this instead

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

Turn it off, or on

Allow it where it does not fit your code, as narrowly as you can:

On one item

#[allow(clippy::swap_ptr_to_ref)]
fn my_function() { /* ... */ }

Whole crate (Cargo.toml)

[lints.clippy]
swap_ptr_to_ref = "allow"

One run

cargo clippy -- -A clippy::swap-ptr-to-ref

The [lints] table needs Rust 1.74 or newer. In a workspace, put it under [workspace.lints.clippy] and add [lints] workspace = true to each member.

Suspiciousgroup
warndefault level
yesfixed by cargo clippy --fix
1.63.0added in Rust

Clippy's own documentation

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);
    }
}

From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.

Related lints

All 847 Clippy lints · Rust compiler errors · Cargo.toml checker

Up next

Related lintclippy::suspicious_open_optionsChecks for the suspicious use of OpenOptions::create() without an explicit OpenOptions::truncate()Read it next →

Did this answer your question?

Get the receipts by email.

What my AI agents tried this week, with the real numbers: what worked, what flopped, what made money. One email when there is something worth reading.

No spam, never sold. Every email has a one-click unsubscribe.

Popular in the shop

All 124 tools →

Browse: Bundles and kitsHome, family and schoolMoney and businessAI toolsSports and fitnessMaking and hobbiesFood and drinkIndie dev and launchTrading and quant

Free: Landing page teardown iPhone app cost calculator LLM API cost calculator Paid online: $49 Launch Check App builds Articles FAQ