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-refThe [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.
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
- suspicious_open_optionsChecks for the suspicious use of OpenOptions::create() without an explicit OpenOptions::truncate()
- suspicious_to_ownedChecks for the usage of _.to_owned(), on a Cow<'_, _>
- suspicious_unary_op_formattingby not having a space between `&&` and `!` it looks like `&&!` is a single operator
- test_attr_in_doctestunit tests in doctest are not executed
- type_id_on_boxcalling `.type_id()` on `Box<dyn Any>`
- unconditional_recursionfunction cannot return without recursing
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?