Skip to content

False positive in undocumented_unsafe_blocks (stable-to-stable regression) #9142

Closed
@shssoichiro

Description

@shssoichiro

Summary

When defining a variable using an unsafe block, if the unsafe block is not on the same line as the variable definition, the undocumented_unsafe_blocks triggers a warning when enabled. This did not trigger the lint in Rust 1.61. It does trigger the lint in Rust 1.62 as well as the latest nightly.

Lint Name

undocumented_unsafe_blocks

Reproducer

I tried this code:

// SAFETY: We write to the array below before reading from it.
let mut col_coeffs_backing: Aligned<[i32; 64]> =
  unsafe { Aligned::uninitialized() };

I saw this happen:

warning: unsafe block missing a safety comment
   --> src/transform/forward.rs:129:9
    |
129 |         unsafe { Aligned::uninitialized() };
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
note: the lint level is defined here
   --> src/lib.rs:69:9
    |
69  | #![warn(clippy::undocumented_unsafe_blocks)]
    |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    = 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

I expected to see no warning.

In Rust 1.61, there is no warning, as expected. Additionally, keeping the code on one line, or moving the SAFETY comment down one line, both resolve the warning. e.g. the following two examples both produce no warning:

// SAFETY: We write to the array below before reading from it.
let mut col_coeffs_backing: Aligned<[i32; 64]> = unsafe { Aligned::uninitialized() };
let mut col_coeffs_backing: Aligned<[i32; 64]> =
  // SAFETY: We write to the array below before reading from it.
  unsafe { Aligned::uninitialized() };

Version

Reproducible on both:

rustc 1.62.0 (a8314ef7d 2022-06-27)
binary: rustc
commit-hash: a8314ef7d0ec7b75c336af2c9857bfaf43002bfc
commit-date: 2022-06-27
host: x86_64-unknown-linux-gnu
release: 1.62.0
LLVM version: 14.0.5


and:

rustc 1.64.0-nightly (06754d885 2022-07-08)
binary: rustc
commit-hash: 06754d8852bea286a3a76d373ccd17e66afb5a8b
commit-date: 2022-07-08
host: x86_64-unknown-linux-gnu
release: 1.64.0-nightly
LLVM version: 14.0.6

Additional Labels

@rustbot +label regression-from-stable-to-nightly regression-from-stable-to-stable

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

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