Email me

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-mut

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.

Nurserygroup
allowdefault level
nofixed by cargo clippy --fix
1.66.0added in Rust

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

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?

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