rust-lang / rust-lang/rust-clippy

`missing_safety_doc` lint doesn't trigger on `unsafe extern "C" fn`

Open
#14,359 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-false-negative
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

By definition, unsafe extern "C" fns are exposed to the outside world, and can therefore be regarded as if they're pub unsafe fns. It is very desirable to be able to require unsafe extern "C" fns be annotated with safety docs. Currently, the missing_safety_doc lint does not trigger for unsafe extern "C" fns, wheras it does trigger for pub unsafe extern "C" fns.

Lint Name

missing_safety_doc

Reproducer

I tried this code:

unsafe extern "C" fn say_hello(msg: *const Cstr) {
   todo!()
}

I expected to see this happen:

warning: unsafe function's docs are missing a `# Safety` section
   --> trie_rs/src/ffi.rs:329:1
    |
329 | unsafe extern "C" fn say_hello(msg: *const u8) {
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#missing_safety_doc
    = note: `#[warn(clippy::missing_safety_doc)]` on by default

Instead, this happened:

No complaints by Clippy whatsoever

Version
rustc 1.85.0 (4d91de4e4 2025-02-17)
binary: rustc
commit-hash: 4d91de4e48198da2e33413efdcd9cd2cc0c46688
commit-date: 2025-02-17
host: aarch64-apple-darwin
release: 1.85.0
LLVM version: 19.1.7

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the missing_safety_doc lint and run the unsafe extern "C" fn reproducer from the issue. Trace how the lint decides which functions require safety documentation, then add coverage so this case reports a missing # Safety section while existing behavior remains unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.