Skip to content

unnecessary_safety_comment false positive in macro #10084

@CeleritasCelery

Description

@CeleritasCelery

Summary

False positive with the unnecessary_safety_comment when the unsafe block is in a macro.

Lint Name

unnecessary_safety_comment

Reproducer

I tried this code:

#![warn(clippy::unnecessary_safety_comment)]
#![warn(clippy::undocumented_unsafe_blocks)]

unsafe fn bar() -> i32 {
    42
}

macro_rules! foo {
    () => {
        // SAFETY: This is safe
        unsafe { bar() }
    };
}

fn main() {
    foo!();
}

I saw this happen:

warning: expression has unnecessary safety comment
  --> src/main.rs:11:18
   |
11 |         unsafe { bar() }
   |                  ^^^^^
...
16 |     foo!();
   |     ------ in this macro invocation
   |
help: consider removing the safety comment
  --> src/main.rs:10:9
   |
10 |         // SAFETY: This is safe
   |         ^^^^^^^^^^^^^^^^^^^^^^^
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_safety_comment
note: the lint level is defined here
  --> src/main.rs:1:9
   |
1  | #![warn(clippy::unnecessary_safety_comment)]
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   = note: this warning originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info)

I expected to see this happen:

No warning triggered

Version

rustc 1.67.0-beta.2 (352eb59a4 2022-12-13)
binary: rustc
commit-hash: 352eb59a4c33abf739914422f2ad975925750146
commit-date: 2022-12-13
host: aarch64-apple-darwin
release: 1.67.0-beta.2
LLVM version: 15.0.6

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions