Skip to content

Commit b8c3f64

Browse files
committed
Add some more test cases for undocumented_unsafe_blocks
1 parent a116b9b commit b8c3f64

File tree

3 files changed

+85
-7
lines changed

3 files changed

+85
-7
lines changed

clippy_lints/src/undocumented_unsafe_blocks.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ declare_clippy_lint! {
8282
///
8383
/// let ptr = NonNull::new(a).unwrap();
8484
/// ```
85-
#[clippy::version = "1.66.0"]
85+
#[clippy::version = "1.67.0"]
8686
pub UNNECESSARY_SAFETY_COMMENT,
8787
restriction,
8888
"creating an unsafe block without explaining why it is safe"
@@ -136,6 +136,7 @@ impl LateLintPass<'_> for UndocumentedUnsafeBlocks {
136136

137137
let item_has_safety_comment = item_has_safety_comment(cx, item);
138138
match (&item.kind, item_has_safety_comment) {
139+
// lint unsafe impl without safety comment
139140
(hir::ItemKind::Impl(impl_), HasSafetyComment::No) if impl_.unsafety == hir::Unsafety::Unsafe => {
140141
if !is_lint_allowed(cx, UNDOCUMENTED_UNSAFE_BLOCKS, item.hir_id())
141142
&& !is_unsafe_from_proc_macro(cx, item.span)
@@ -157,6 +158,7 @@ impl LateLintPass<'_> for UndocumentedUnsafeBlocks {
157158
);
158159
}
159160
},
161+
// lint safe impl with unnecessary safety comment
160162
(hir::ItemKind::Impl(impl_), HasSafetyComment::Yes(pos)) if impl_.unsafety == hir::Unsafety::Normal => {
161163
if !is_lint_allowed(cx, UNNECESSARY_SAFETY_COMMENT, item.hir_id()) {
162164
let (span, help_span) = mk_spans(pos);
@@ -172,6 +174,7 @@ impl LateLintPass<'_> for UndocumentedUnsafeBlocks {
172174
}
173175
},
174176
(hir::ItemKind::Impl(_), _) => {},
177+
// const and static items only need a safety comment if their body is an unsafe block, lint otherwise
175178
(&hir::ItemKind::Const(.., body) | &hir::ItemKind::Static(.., body), HasSafetyComment::Yes(pos)) => {
176179
if !is_lint_allowed(cx, UNNECESSARY_SAFETY_COMMENT, body.hir_id) {
177180
let body = cx.tcx.hir().body(body);
@@ -192,6 +195,8 @@ impl LateLintPass<'_> for UndocumentedUnsafeBlocks {
192195
}
193196
}
194197
},
198+
// Aside from unsafe impls and consts/statics with an unsafe block, items in general
199+
// do not have safety invariants that need to be documented, so lint those.
195200
(_, HasSafetyComment::Yes(pos)) => {
196201
if !is_lint_allowed(cx, UNNECESSARY_SAFETY_COMMENT, item.hir_id()) {
197202
let (span, help_span) = mk_spans(pos);

tests/ui/undocumented_unsafe_blocks.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,19 @@ mod unsafe_impl_invalid_comment {
472472
unsafe impl Interference for () {}
473473
}
474474

475+
mod unsafe_items_invalid_comment {
476+
// SAFETY:
477+
const CONST: u32 = 0;
478+
// SAFETY:
479+
static STATIC: u32 = 0;
480+
// SAFETY:
481+
struct Struct;
482+
// SAFETY:
483+
enum Enum {}
484+
// SAFETY:
485+
mod module {}
486+
}
487+
475488
unsafe trait ImplInFn {}
476489

477490
fn impl_in_fn() {

tests/ui/undocumented_unsafe_blocks.stderr

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -260,45 +260,105 @@ LL | unsafe impl Interference for () {}
260260
|
261261
= help: consider adding a safety comment on the preceding line
262262

263-
error: unsafe impl missing a safety comment
263+
error: constant item has unnecessary safety comment
264+
--> $DIR/undocumented_unsafe_blocks.rs:477:5
265+
|
266+
LL | const CONST: u32 = 0;
267+
| ^^^^^^^^^^^^^^^^^^^^^
268+
|
269+
help: consider removing the safety comment
270+
--> $DIR/undocumented_unsafe_blocks.rs:476:5
271+
|
272+
LL | // SAFETY:
273+
| ^^^^^^^^^^
274+
275+
error: static item has unnecessary safety comment
264276
--> $DIR/undocumented_unsafe_blocks.rs:479:5
265277
|
278+
LL | static STATIC: u32 = 0;
279+
| ^^^^^^^^^^^^^^^^^^^^^^^
280+
|
281+
help: consider removing the safety comment
282+
--> $DIR/undocumented_unsafe_blocks.rs:478:5
283+
|
284+
LL | // SAFETY:
285+
| ^^^^^^^^^^
286+
287+
error: struct has unnecessary safety comment
288+
--> $DIR/undocumented_unsafe_blocks.rs:481:5
289+
|
290+
LL | struct Struct;
291+
| ^^^^^^^^^^^^^^
292+
|
293+
help: consider removing the safety comment
294+
--> $DIR/undocumented_unsafe_blocks.rs:480:5
295+
|
296+
LL | // SAFETY:
297+
| ^^^^^^^^^^
298+
299+
error: enum has unnecessary safety comment
300+
--> $DIR/undocumented_unsafe_blocks.rs:483:5
301+
|
302+
LL | enum Enum {}
303+
| ^^^^^^^^^^^^
304+
|
305+
help: consider removing the safety comment
306+
--> $DIR/undocumented_unsafe_blocks.rs:482:5
307+
|
308+
LL | // SAFETY:
309+
| ^^^^^^^^^^
310+
311+
error: module has unnecessary safety comment
312+
--> $DIR/undocumented_unsafe_blocks.rs:485:5
313+
|
314+
LL | mod module {}
315+
| ^^^^^^^^^^^^^
316+
|
317+
help: consider removing the safety comment
318+
--> $DIR/undocumented_unsafe_blocks.rs:484:5
319+
|
320+
LL | // SAFETY:
321+
| ^^^^^^^^^^
322+
323+
error: unsafe impl missing a safety comment
324+
--> $DIR/undocumented_unsafe_blocks.rs:492:5
325+
|
266326
LL | unsafe impl ImplInFn for () {}
267327
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
268328
|
269329
= help: consider adding a safety comment on the preceding line
270330

271331
error: unsafe impl missing a safety comment
272-
--> $DIR/undocumented_unsafe_blocks.rs:488:1
332+
--> $DIR/undocumented_unsafe_blocks.rs:501:1
273333
|
274334
LL | unsafe impl CrateRoot for () {}
275335
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
276336
|
277337
= help: consider adding a safety comment on the preceding line
278338

279339
error: unsafe block missing a safety comment
280-
--> $DIR/undocumented_unsafe_blocks.rs:498:9
340+
--> $DIR/undocumented_unsafe_blocks.rs:511:9
281341
|
282342
LL | unsafe {};
283343
| ^^^^^^^^^
284344
|
285345
= help: consider adding a safety comment on the preceding line
286346

287347
error: unsafe block missing a safety comment
288-
--> $DIR/undocumented_unsafe_blocks.rs:502:12
348+
--> $DIR/undocumented_unsafe_blocks.rs:515:12
289349
|
290350
LL | if unsafe { true } {
291351
| ^^^^^^^^^^^^^^^
292352
|
293353
= help: consider adding a safety comment on the preceding line
294354

295355
error: unsafe block missing a safety comment
296-
--> $DIR/undocumented_unsafe_blocks.rs:505:23
356+
--> $DIR/undocumented_unsafe_blocks.rs:518:23
297357
|
298358
LL | let bar = unsafe {};
299359
| ^^^^^^^^^
300360
|
301361
= help: consider adding a safety comment on the preceding line
302362

303-
error: aborting due to 35 previous errors
363+
error: aborting due to 40 previous errors
304364

0 commit comments

Comments
 (0)