rust-lang / rust-lang/rust-clippy

`unnecessary_safety_comment` inconsistency with wrong-style safety section

Open
#15,034 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Summary

unnecessary_safety_comment appears to detect /// SAFETY: ... as a safety comment (i.e. not # Safety section) in a normal function, but it doesn't inside an impl block.

Lint Name

unnecessary_safety_comment

Reproducer

I tried this code:

/// SAFETY: ...
fn f() {}

and it triggers unnecessary_safety_comment. However, this:

struct S;
impl S {
    /// SAFETY: ...
    fn f() {}
}

does not trigger it.

I assume unnecessary_safety_comment is intended to only lint on actual safety comments (i.e. not safety docs/sections), even if they use /// instead of //. If so, then the former is expected, and the latter is a false negative.

Please also see this other suggested lint for consistency on safety comments/docs: https://github.com/rust-lang/rust-clippy/issues/15033. For instance, if the former case above were to be a false positive, one could use that new/suggested lint to catch a case like this where the function was supposed to be unsafe fn. And if the former is indeed expected, then perhaps the message of the lint could be clarified if the user used /// instead of just //.

More combinations: https://godbolt.org/z/43onqxYrh

Version
rustc 1.89.0-nightly (d13a431a6 2025-06-09)
binary: rustc
commit-hash: d13a431a6cc69cd65efe7c3eb7808251d6fd7a46
commit-date: 2025-06-09
host: x86_64-unknown-linux-gnu
release: 1.89.0-nightly
LLVM version: 20.1.5

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 reproducing the two examples for unnecessary_safety_comment: the standalone function triggers the lint, while the method inside impl S does not. Compare the behavior with the additional combinations in the linked Godbolt example and determine the intended handling of wrong-style safety comments before defining the completed fix.

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
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.