Skip to content

Commit f96dd38

Browse files
committed
Address reviews
1 parent 9c69e1c commit f96dd38

5 files changed

+178
-157
lines changed

clippy_lints/src/undocumented_unsafe_blocks.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ declare_clippy_lint! {
8989
#[clippy::version = "1.67.0"]
9090
pub UNNECESSARY_SAFETY_COMMENT,
9191
restriction,
92-
"creating an unsafe block without explaining why it is safe"
92+
"annotating safe code with a safety comment"
9393
}
9494

9595
declare_lint_pass!(UndocumentedUnsafeBlocks => [UNDOCUMENTED_UNSAFE_BLOCKS, UNNECESSARY_SAFETY_COMMENT]);
@@ -138,12 +138,11 @@ impl<'tcx> LateLintPass<'tcx> for UndocumentedUnsafeBlocks {
138138
}
139139

140140
fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &hir::Stmt<'tcx>) {
141-
let expr = match stmt.kind {
141+
let (
142142
hir::StmtKind::Local(&hir::Local { init: Some(expr), .. })
143143
| hir::StmtKind::Expr(expr)
144-
| hir::StmtKind::Semi(expr) => expr,
145-
_ => return,
146-
};
144+
| hir::StmtKind::Semi(expr)
145+
) = stmt.kind else { return };
147146
if !is_lint_allowed(cx, UNNECESSARY_SAFETY_COMMENT, stmt.hir_id)
148147
&& !in_external_macro(cx.tcx.sess, stmt.span)
149148
&& let HasSafetyComment::Yes(pos) = stmt_has_safety_comment(cx, stmt.span, stmt.hir_id)

tests/ui/undocumented_unsafe_blocks.rs

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -472,19 +472,6 @@ 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-
488475
unsafe trait ImplInFn {}
489476

490477
fn impl_in_fn() {
@@ -522,35 +509,4 @@ fn issue_9142() {
522509
};
523510
}
524511

525-
mod unnecessary_from_macro {
526-
trait T {}
527-
528-
macro_rules! no_safety_comment {
529-
($t:ty) => {
530-
impl T for $t {}
531-
};
532-
}
533-
534-
// FIXME: This is not caught
535-
// Safety: unnecessary
536-
no_safety_comment!(());
537-
538-
macro_rules! with_safety_comment {
539-
($t:ty) => {
540-
// Safety: unnecessary
541-
impl T for $t {}
542-
};
543-
}
544-
545-
with_safety_comment!(i32);
546-
}
547-
548-
fn unnecessary_on_stmt_and_expr() -> u32 {
549-
// SAFETY: unnecessary
550-
let num = 42;
551-
552-
// SAFETY: unnecessary
553-
24
554-
}
555-
556512
fn main() {}

tests/ui/undocumented_unsafe_blocks.stderr

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

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
276-
--> $DIR/undocumented_unsafe_blocks.rs:479:5
277-
|
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-
323263
error: unsafe impl missing a safety comment
324-
--> $DIR/undocumented_unsafe_blocks.rs:492:5
264+
--> $DIR/undocumented_unsafe_blocks.rs:479:5
325265
|
326266
LL | unsafe impl ImplInFn for () {}
327267
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
328268
|
329269
= help: consider adding a safety comment on the preceding line
330270

331271
error: unsafe impl missing a safety comment
332-
--> $DIR/undocumented_unsafe_blocks.rs:501:1
272+
--> $DIR/undocumented_unsafe_blocks.rs:488:1
333273
|
334274
LL | unsafe impl CrateRoot for () {}
335275
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
336276
|
337277
= help: consider adding a safety comment on the preceding line
338278

339279
error: unsafe block missing a safety comment
340-
--> $DIR/undocumented_unsafe_blocks.rs:511:9
280+
--> $DIR/undocumented_unsafe_blocks.rs:498:9
341281
|
342282
LL | unsafe {};
343283
| ^^^^^^^^^
344284
|
345285
= help: consider adding a safety comment on the preceding line
346286

347287
error: statement has unnecessary safety comment
348-
--> $DIR/undocumented_unsafe_blocks.rs:514:5
288+
--> $DIR/undocumented_unsafe_blocks.rs:501:5
349289
|
350290
LL | / let _ = {
351291
LL | | if unsafe { true } {
@@ -357,66 +297,26 @@ LL | | };
357297
| |______^
358298
|
359299
help: consider removing the safety comment
360-
--> $DIR/undocumented_unsafe_blocks.rs:513:5
300+
--> $DIR/undocumented_unsafe_blocks.rs:500:5
361301
|
362302
LL | // SAFETY: this is more than one level away, so it should warn
363303
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
364304

365305
error: unsafe block missing a safety comment
366-
--> $DIR/undocumented_unsafe_blocks.rs:515:12
306+
--> $DIR/undocumented_unsafe_blocks.rs:502:12
367307
|
368308
LL | if unsafe { true } {
369309
| ^^^^^^^^^^^^^^^
370310
|
371311
= help: consider adding a safety comment on the preceding line
372312

373313
error: unsafe block missing a safety comment
374-
--> $DIR/undocumented_unsafe_blocks.rs:518:23
314+
--> $DIR/undocumented_unsafe_blocks.rs:505:23
375315
|
376316
LL | let bar = unsafe {};
377317
| ^^^^^^^^^
378318
|
379319
= help: consider adding a safety comment on the preceding line
380320

381-
error: impl has unnecessary safety comment
382-
--> $DIR/undocumented_unsafe_blocks.rs:541:13
383-
|
384-
LL | impl T for $t {}
385-
| ^^^^^^^^^^^^^^^^
386-
...
387-
LL | with_safety_comment!(i32);
388-
| ------------------------- in this macro invocation
389-
|
390-
help: consider removing the safety comment
391-
--> $DIR/undocumented_unsafe_blocks.rs:540:13
392-
|
393-
LL | // Safety: unnecessary
394-
| ^^^^^^^^^^^^^^^^^^^^^^
395-
= note: this error originates in the macro `with_safety_comment` (in Nightly builds, run with -Z macro-backtrace for more info)
396-
397-
error: expression has unnecessary safety comment
398-
--> $DIR/undocumented_unsafe_blocks.rs:553:5
399-
|
400-
LL | 24
401-
| ^^
402-
|
403-
help: consider removing the safety comment
404-
--> $DIR/undocumented_unsafe_blocks.rs:552:5
405-
|
406-
LL | // SAFETY: unnecessary
407-
| ^^^^^^^^^^^^^^^^^^^^^^
408-
409-
error: statement has unnecessary safety comment
410-
--> $DIR/undocumented_unsafe_blocks.rs:550:5
411-
|
412-
LL | let num = 42;
413-
| ^^^^^^^^^^^^^
414-
|
415-
help: consider removing the safety comment
416-
--> $DIR/undocumented_unsafe_blocks.rs:549:5
417-
|
418-
LL | // SAFETY: unnecessary
419-
| ^^^^^^^^^^^^^^^^^^^^^^
420-
421-
error: aborting due to 44 previous errors
321+
error: aborting due to 36 previous errors
422322

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#![warn(clippy::undocumented_unsafe_blocks, clippy::unnecessary_safety_comment)]
2+
#![allow(clippy::let_unit_value, clippy::missing_safety_doc)]
3+
4+
mod unsafe_items_invalid_comment {
5+
// SAFETY:
6+
const CONST: u32 = 0;
7+
// SAFETY:
8+
static STATIC: u32 = 0;
9+
// SAFETY:
10+
struct Struct;
11+
// SAFETY:
12+
enum Enum {}
13+
// SAFETY:
14+
mod module {}
15+
}
16+
17+
mod unnecessary_from_macro {
18+
trait T {}
19+
20+
macro_rules! no_safety_comment {
21+
($t:ty) => {
22+
impl T for $t {}
23+
};
24+
}
25+
26+
// FIXME: This is not caught
27+
// Safety: unnecessary
28+
no_safety_comment!(());
29+
30+
macro_rules! with_safety_comment {
31+
($t:ty) => {
32+
// Safety: unnecessary
33+
impl T for $t {}
34+
};
35+
}
36+
37+
with_safety_comment!(i32);
38+
}
39+
40+
fn unnecessary_on_stmt_and_expr() -> u32 {
41+
// SAFETY: unnecessary
42+
let num = 42;
43+
44+
// SAFETY: unnecessary
45+
if num > 24 {}
46+
47+
// SAFETY: unnecessary
48+
24
49+
}
50+
51+
fn main() {}

0 commit comments

Comments
 (0)