Skip to content

rustc does not warn in a safe fn when a trivially unsafe operation is used on an argument #162090

Description

@ds84182

Code

fn oopsie_woopsie_uwu(ptr: *mut u32) {
    unsafe { ptr.write(123); }
}

fn main() {
    oopsie_woopsie_uwu(0 as _);
}

Current output

No diagnostics

Desired output

warning: call to function `std::ptr::mut_ptr::<impl *mut T>::write` is unsafe but the pointer is an argument of a safe function
 --> src/main.rs:2:7
  |
1 | fn oopsie_woopsie_uwu(ptr: *mut u32) {
  |                       ^^^^^^^^^^^^^
  |                       argument declared here
  |
2 |     { ptr.write(123); }
  |       ^^^^^^^^^^^^^^ call to unsafe function
  |
  = note: consult the function's documentation for information on how to avoid undefined behavior

Rationale and extra context

I propose a simple lint warning the developer that the function should be marked unsafe, otherwise preconditions are easily ignored. For now this should only include integers (unchecked_* functions) and pointers (read, write, arithmetic). It should not include struct fields unless the field is marked as pub and is mutable.

It could also include FFI functions that accept a pointer but are marked as safe.

Other cases

Rust Version

1.98.0 (playground)

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions