rust-lang / rust-clippy

A bunch of lints to catch common mistakes and improve your Rust code. Book: https://doc.rust-lang.org/clippy/
https://rust-lang.github.io/rust-clippy/
Other
10.89k stars 1.46k forks source link

`undocumented_unsafe_blocks` doesn't recognise safety comments before `const`s in `impl` blocks #12720

Open Spartan2909 opened 2 weeks ago

Spartan2909 commented 2 weeks ago

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

J-ZhengLi commented 3 days ago

duplicate of #11709 , which will hopefully be fixed after #12672 land~