rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

Clippy does not find SAFETY comment in splitted line

Open TriedAngle opened this issue 1 month ago • 0 comments

Summary

clippy is unable to find the the Safety comment when splitting the line, for example when using cargo fmt.

Lint Name

clippy::undocumented_unsafe_blocks

Reproducer

use std::borrow::Cow;
pub struct Id(Cow<'static, str>);

impl<'a> From<&'a str> for Id {
    fn from(s: &'a str) -> Self {
        // SAFETY: The caller guarantees that the source data (e.g., the source file)
        // stays in memory for the duration of the program or these objects' lifetime.
        let s_static: &'static str = 
            unsafe { std::mem::transmute::<&'a str, &'static str>(s) };

        Self(Cow::Borrowed(s_static))
    }
}

the clippy output (I am actually using a Macro)

error: unsafe block missing a safety comment
   --> spirv_new\src\lib.rs:145:21
    |
145 |                     unsafe { std::mem::transmute::<&'a str, &'static str>(s) };
    |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
176 | impl_cow_wrapper!(SpirvString);
    | ------------------------------ in this macro invocation
    |
    = help: consider adding a safety comment on the preceding line
    = help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.91.0/index.html#undocumented_unsafe_blocks
    = note: this error originates in the macro `impl_cow_wrapper` (in Nightly builds, run with -Z macro-backtrace for more info)

Version

rustc 1.91.1 (ed61e7d7e 2025-11-07)
binary: rustc
commit-hash: ed61e7d7e242494fb7057f2657300d9e77bb4fcb
commit-date: 2025-11-07
host: x86_64-pc-windows-msvc
release: 1.91.1
LLVM version: 21.1.2

Additional Labels

No response

TriedAngle avatar Dec 10 '25 15:12 TriedAngle