RS.CLIPPY.PTR_AS_PTR

Casting using `as` between raw pointers that doesn't change their constness, where `pointer::cast` could take the place of `as`

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

What it does

Checks for as casts between raw pointers that don't change their constness, namely *const T to *const U and *mut T to *mut U.

Why is this bad?

Though as casts between raw pointers are not terrible, pointer::cast is safer because it cannot accidentally change the pointer's mutability, nor cast the pointer to other types like usize.

Example

let ptr: *const u32 = &42_u32;
let mut_ptr: *mut u32 = &mut 42_u32;
let _ = ptr as *const i32;
let _ = mut_ptr as *mut i32;

Use instead:

let ptr: *const u32 = &42_u32;
let mut_ptr: *mut u32 = &mut 42_u32;
let _ = ptr.cast::<i32>();
let _ = mut_ptr.cast::<i32>();

Configuration

  • msrv: The minimum rust version that the project supports. Defaults to the rust-version field in Cargo.toml

    (default: current version)