Skip to content

undocumented_unsafe_blocks doesn't recognise safety comments before consts in impl blocks #12720

@Spartan2909

Description

@Spartan2909

Summary

undocumented_unsafe_blocks lints on documented unsafe blocks that constitute the body of a const in an impl block.

I get the expected behaviour if I put both the unsafe block and the safety comment into a block expression, or if I remove the item from the impl block.

Lint Name

undocumented_unsafe_blocks

Reproducer

I tried this code:

pub struct SourceId(NonZeroU64);

impl SourceId {
    // SAFETY: 1 is not 0.
    const DUMMY: SourceId = unsafe { SourceId(NonZeroU64::new_unchecked(1)) };
}

I saw this happen:

error: unsafe block missing a safety comment
  --> continuate-error\src\lib.rs:20:29
   |
20 |     const DUMMY: SourceId = unsafe { SourceId(NonZeroU64::new_unchecked(1)) };
   |                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = help: consider adding a safety comment on the preceding line
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#undocumented_unsafe_blocks
   = note: requested on the command line with `-D clippy::undocumented-unsafe-blocks`

I expected to see this happen:

No output.

Version

rustc 1.79.0-nightly (ef8b9dcf2 2024-04-24)
binary: rustc
commit-hash: ef8b9dcf23700f2e2265317611460d3a65c19eff
commit-date: 2024-04-24
host: x86_64-pc-windows-msvc
release: 1.79.0-nightly
LLVM version: 18.1.4

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