Email me

Clippy lints / Correctness

Clippy lint · Correctness · checked with Clippy 0.1.99

clippy::not_unsafe_ptr_arg_deref: this public function might dereference a raw pointer but is not marked `unsafe`

What it meansChecks for public functions that dereference raw pointer arguments but are not marked unsafe. It is in the correctness group and denied by default (the build fails).

What you see

error: this public function might dereference a raw pointer but is not marked `unsafe`
 --> main.rs:2:30
  |
2 |     println!("{}", unsafe { *x });
  |                              ^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#not_unsafe_ptr_arg_deref
  = note: `#[deny(clippy::not_unsafe_ptr_arg_deref)]` 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?

The function should almost definitely be marked unsafe, since for an arbitrary raw pointer, there is no way of telling for sure if it is valid. In general, this lint should never be disabled unless it is definitely a false positive (please submit an issue if so) since it breaks Rust’s soundness guarantees, directly exposing API users to potentially dangerous program behavior. This is also true for internal APIs, as it is easy to leak unsoundness.

Code that is almost certainly wrong. Denied by default: the build fails until you fix it or allow it.

How to fix it

Clippy does not fix this one automatically. Change the code by hand:

Flagged

pub fn foo(x: *const u8) {
    println!("{}", unsafe { *x });
}

// this call "looks" safe but will segfault or worse!
// foo(invalid_ptr);

Write this instead

pub unsafe fn foo(x: *const u8) {
    println!("{}", unsafe { *x });
}

// this would cause a compiler error for calling without `unsafe`
// foo(invalid_ptr);

// sound call if the caller knows the pointer is valid
unsafe { foo(valid_ptr); }

Turn it off, or on

Allow it where it does not fit your code, as narrowly as you can:

On one item

#[allow(clippy::not_unsafe_ptr_arg_deref)]
fn my_function() { /* ... */ }

Whole crate (Cargo.toml)

[lints.clippy]
not_unsafe_ptr_arg_deref = "allow"

One run

cargo clippy -- -A clippy::not-unsafe-ptr-arg-deref

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.

Correctnessgroup
denydefault level
nofixed by cargo clippy --fix
pre 1.29.0added in Rust

Clippy's own documentation

What it does

Checks for public functions that dereference raw pointer arguments but are not marked unsafe.

Why is this bad?

The function should almost definitely be marked unsafe, since for an arbitrary raw pointer, there is no way of telling for sure if it is valid.

In general, this lint should never be disabled unless it is definitely a false positive (please submit an issue if so) since it breaks Rust’s soundness guarantees, directly exposing API users to potentially dangerous program behavior. This is also true for internal APIs, as it is easy to leak unsoundness.

Context

In Rust, an unsafe {...} block is used to indicate that the code in that section has been verified in some way that the compiler can not. For a function that accepts a raw pointer then accesses the pointer’s data, this is generally impossible as the incoming pointer could point anywhere, valid or not. So, the signature should be marked unsafe fn: this indicates that the function’s caller must provide some verification that the arguments it sends are valid (and then call the function within an unsafe block).

Known problems

  • It does not check functions recursively so if the pointer is passed to a private non-unsafe function which does the dereferencing, the lint won’t trigger (false negative).
  • It only checks for arguments whose type are raw pointers, not raw pointers got from an argument in some other way (fn foo(bar: &[*const u8]) or some_argument.get_raw_ptr()) (false negative).

Example

pub fn foo(x: *const u8) {
    println!("{}", unsafe { *x });
}

// this call "looks" safe but will segfault or worse!
// foo(invalid_ptr);

Use instead:

pub unsafe fn foo(x: *const u8) {
    println!("{}", unsafe { *x });
}

// this would cause a compiler error for calling without `unsafe`
// foo(invalid_ptr);

// sound call if the caller knows the pointer is valid
unsafe { foo(valid_ptr); }

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::never_loopthis loop never actually loopsRead 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