Add some more test cases for undocumented_unsafe_blocks

This commit is contained in:
Lukas Wirth 2022-11-17 11:00:51 +01:00
parent a116b9bdba
commit b8c3f64cee
3 changed files with 85 additions and 7 deletions

View File

@ -82,7 +82,7 @@ declare_clippy_lint! {
///
/// let ptr = NonNull::new(a).unwrap();
/// ```
#[clippy::version = "1.66.0"]
#[clippy::version = "1.67.0"]
pub UNNECESSARY_SAFETY_COMMENT,
restriction,
"creating an unsafe block without explaining why it is safe"
@ -136,6 +136,7 @@ impl LateLintPass<'_> for UndocumentedUnsafeBlocks {
let item_has_safety_comment = item_has_safety_comment(cx, item);
match (&item.kind, item_has_safety_comment) {
// lint unsafe impl without safety comment
(hir::ItemKind::Impl(impl_), HasSafetyComment::No) if impl_.unsafety == hir::Unsafety::Unsafe => {
if !is_lint_allowed(cx, UNDOCUMENTED_UNSAFE_BLOCKS, item.hir_id())
&& !is_unsafe_from_proc_macro(cx, item.span)
@ -157,6 +158,7 @@ impl LateLintPass<'_> for UndocumentedUnsafeBlocks {
);
}
},
// lint safe impl with unnecessary safety comment
(hir::ItemKind::Impl(impl_), HasSafetyComment::Yes(pos)) if impl_.unsafety == hir::Unsafety::Normal => {
if !is_lint_allowed(cx, UNNECESSARY_SAFETY_COMMENT, item.hir_id()) {
let (span, help_span) = mk_spans(pos);
@ -172,6 +174,7 @@ impl LateLintPass<'_> for UndocumentedUnsafeBlocks {
}
},
(hir::ItemKind::Impl(_), _) => {},
// const and static items only need a safety comment if their body is an unsafe block, lint otherwise
(&hir::ItemKind::Const(.., body) | &hir::ItemKind::Static(.., body), HasSafetyComment::Yes(pos)) => {
if !is_lint_allowed(cx, UNNECESSARY_SAFETY_COMMENT, body.hir_id) {
let body = cx.tcx.hir().body(body);
@ -192,6 +195,8 @@ impl LateLintPass<'_> for UndocumentedUnsafeBlocks {
}
}
},
// Aside from unsafe impls and consts/statics with an unsafe block, items in general
// do not have safety invariants that need to be documented, so lint those.
(_, HasSafetyComment::Yes(pos)) => {
if !is_lint_allowed(cx, UNNECESSARY_SAFETY_COMMENT, item.hir_id()) {
let (span, help_span) = mk_spans(pos);

View File

@ -472,6 +472,19 @@ mod unsafe_impl_invalid_comment {
unsafe impl Interference for () {}
}
mod unsafe_items_invalid_comment {
// SAFETY:
const CONST: u32 = 0;
// SAFETY:
static STATIC: u32 = 0;
// SAFETY:
struct Struct;
// SAFETY:
enum Enum {}
// SAFETY:
mod module {}
}
unsafe trait ImplInFn {}
fn impl_in_fn() {

View File

@ -260,16 +260,76 @@ LL | unsafe impl Interference for () {}
|
= help: consider adding a safety comment on the preceding line
error: unsafe impl missing a safety comment
error: constant item has unnecessary safety comment
--> $DIR/undocumented_unsafe_blocks.rs:477:5
|
LL | const CONST: u32 = 0;
| ^^^^^^^^^^^^^^^^^^^^^
|
help: consider removing the safety comment
--> $DIR/undocumented_unsafe_blocks.rs:476:5
|
LL | // SAFETY:
| ^^^^^^^^^^
error: static item has unnecessary safety comment
--> $DIR/undocumented_unsafe_blocks.rs:479:5
|
LL | static STATIC: u32 = 0;
| ^^^^^^^^^^^^^^^^^^^^^^^
|
help: consider removing the safety comment
--> $DIR/undocumented_unsafe_blocks.rs:478:5
|
LL | // SAFETY:
| ^^^^^^^^^^
error: struct has unnecessary safety comment
--> $DIR/undocumented_unsafe_blocks.rs:481:5
|
LL | struct Struct;
| ^^^^^^^^^^^^^^
|
help: consider removing the safety comment
--> $DIR/undocumented_unsafe_blocks.rs:480:5
|
LL | // SAFETY:
| ^^^^^^^^^^
error: enum has unnecessary safety comment
--> $DIR/undocumented_unsafe_blocks.rs:483:5
|
LL | enum Enum {}
| ^^^^^^^^^^^^
|
help: consider removing the safety comment
--> $DIR/undocumented_unsafe_blocks.rs:482:5
|
LL | // SAFETY:
| ^^^^^^^^^^
error: module has unnecessary safety comment
--> $DIR/undocumented_unsafe_blocks.rs:485:5
|
LL | mod module {}
| ^^^^^^^^^^^^^
|
help: consider removing the safety comment
--> $DIR/undocumented_unsafe_blocks.rs:484:5
|
LL | // SAFETY:
| ^^^^^^^^^^
error: unsafe impl missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:492:5
|
LL | unsafe impl ImplInFn for () {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider adding a safety comment on the preceding line
error: unsafe impl missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:488:1
--> $DIR/undocumented_unsafe_blocks.rs:501:1
|
LL | unsafe impl CrateRoot for () {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@ -277,7 +337,7 @@ LL | unsafe impl CrateRoot for () {}
= help: consider adding a safety comment on the preceding line
error: unsafe block missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:498:9
--> $DIR/undocumented_unsafe_blocks.rs:511:9
|
LL | unsafe {};
| ^^^^^^^^^
@ -285,7 +345,7 @@ LL | unsafe {};
= help: consider adding a safety comment on the preceding line
error: unsafe block missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:502:12
--> $DIR/undocumented_unsafe_blocks.rs:515:12
|
LL | if unsafe { true } {
| ^^^^^^^^^^^^^^^
@ -293,12 +353,12 @@ LL | if unsafe { true } {
= help: consider adding a safety comment on the preceding line
error: unsafe block missing a safety comment
--> $DIR/undocumented_unsafe_blocks.rs:505:23
--> $DIR/undocumented_unsafe_blocks.rs:518:23
|
LL | let bar = unsafe {};
| ^^^^^^^^^
|
= help: consider adding a safety comment on the preceding line
error: aborting due to 35 previous errors
error: aborting due to 40 previous errors