RS.CLIPPY.AS_PTR_CAST_MUT
Casting the result of the `&self`-taking `as_ptr` to a mutable pointer
This checker is a Clippy lint created by The Rust Project Contributors. The documentation shown here is a copy of the original documentation for: as_ptr_cast_mut. Copyright ©2025 The Rust Team. All rights reserved.
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) };