Clippy lints / Nursery
Clippy lint · Nursery · checked with Clippy 0.1.99
clippy::as_ptr_cast_mut: casting the result of `as_ptr` to *mut u8
What it meansChecks for the result of a &self-taking as_ptr being cast to a mutable pointer. It is in the nursery group and off by default.
What you see
warning: casting the result of `as_ptr` to *mut u8 --> main.rs:4:11 | 4 | let ptr = vec.as_ptr() as *mut u8; | ^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `vec.as_mut_ptr()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#as_ptr_cast_mut = note: `-W clippy::as-ptr-cast-mut` implied by `-W clippy::nursery`
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?
Since as_ptr takes a &self, the pointer won’t have write permissions unless interior mutability is used, making it unlikely that having it as a mutable pointer is correct.
New lints still being finished. Off by default and may have false positives.
How to fix it
Clippy does not fix this one automatically (its suggestion may change behaviour, so it only shows it). Change the code by hand:
Flagged
let mut vec = Vec::<u8>::with_capacity(1);
let ptr = vec.as_ptr() as *mut u8;
unsafe { ptr.write(4) }; // UNDEFINED BEHAVIOUR
Write this instead
let mut vec = Vec::<u8>::with_capacity(1);
let ptr = vec.as_mut_ptr();
unsafe { ptr.write(4) };
Turn it off, or on
It is off by default. To use it, set it to warn or deny:
Whole crate (Cargo.toml)
[lints.clippy]
as_ptr_cast_mut = "warn"In code
#![warn(clippy::as_ptr_cast_mut)]One run
cargo clippy -- -W clippy::as-ptr-cast-mutThe [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 the result of a &self-taking as_ptr being cast to a mutable pointer.
Why is this bad?
Since as_ptr takes a &self, the pointer won’t have write permissions unless interior
mutability is used, making it unlikely that having it as a mutable pointer is correct.
Example
let mut vec = Vec::<u8>::with_capacity(1);
let ptr = vec.as_ptr() as *mut u8;
unsafe { ptr.write(4) }; // UNDEFINED BEHAVIOUR
Use instead:
let mut vec = Vec::<u8>::with_capacity(1);
let ptr = vec.as_mut_ptr();
unsafe { ptr.write(4) };
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- redundant_cloneChecks for a redundant clone() (and its relatives) which clones an owned value that is going to be dropped without further use
- branches_sharing_codeChecks if the blocks of an if/else, or the arms of a match, contain shared code that can be moved out of the branches
- clear_with_drain`drain` used to clear a `Vec`
- coerce_container_to_anyProtects against unintended coercion of references to container types to &dyn Any when the container type dereferences to a dyn Any which could be directly referenced instead
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::redundant_cloneChecks for a redundant clone() (and its relatives) which clones an owned value that is going to be dropped without further useRead it next →Did this answer your question?