Skip to content

Commit b26a1aa

Browse files
authored
[unnecessary_safety_comment] Some fixes regarding comments above attributes (rust-lang#15678)
The way clippy checked for SAFETY comments above attributes was wrong: - It wasn't considered for items - It cause an ICE with some attributes - When considering comments above attributes, it didn't considered comments below them, causing some unlintable situations. This PR tries to fix this by delegating the skipping of attributes to the function analyzing the source code itself. changelog: [`unnecessary_safety_comment`]: Taking into account comments above attributes for items Fixes rust-lang#14555 Fixes rust-lang#15684 Fixes rust-lang#15754
2 parents eee8ef8 + 9cc0291 commit b26a1aa

13 files changed

+564
-133
lines changed

clippy_lints/src/undocumented_unsafe_blocks.rs

Lines changed: 133 additions & 94 deletions
Large diffs are not rendered by default.

tests/ui-cargo/undocumented_unsafe_blocks/fail/Cargo.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ error: module has unnecessary safety comment
55
| ^^^^^^^^
66
|
77
help: consider removing the safety comment
8-
--> src/main.rs:1:1
8+
--> src/main.rs:1:4
99
|
1010
1 | // SAFETY: ...
11-
| ^^^^^^^^^^^^^^
11+
| ^^^^^^^
1212
= note: requested on the command line with `-D clippy::unnecessary-safety-comment`
1313

1414
error: module has unnecessary safety comment
@@ -18,9 +18,9 @@ error: module has unnecessary safety comment
1818
| ^^^^^^^^
1919
|
2020
help: consider removing the safety comment
21-
--> src/main.rs:4:1
21+
--> src/main.rs:4:4
2222
|
2323
4 | // SAFETY: ...
24-
| ^^^^^^^^^^^^^^
24+
| ^^^^^^^
2525

2626
error: could not compile `undocumented_unsafe_blocks` (bin "undocumented_unsafe_blocks") due to 2 previous errors

tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.default.stderr

Lines changed: 128 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,10 @@ LL | const BIG_NUMBER: i32 = 1000000;
247247
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
248248
|
249249
help: consider removing the safety comment
250-
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:507:5
250+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:507:8
251251
|
252252
LL | // SAFETY:
253-
| ^^^^^^^^^^
253+
| ^^^^^^^
254254
= note: `-D clippy::unnecessary-safety-comment` implied by `-D warnings`
255255
= help: to override `-D warnings` add `#[allow(clippy::unnecessary_safety_comment)]`
256256

@@ -289,10 +289,10 @@ LL | | };
289289
| |______^
290290
|
291291
help: consider removing the safety comment
292-
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:542:5
292+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:542:8
293293
|
294294
LL | // SAFETY: this is more than one level away, so it should warn
295-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
295+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
296296

297297
error: unsafe block missing a safety comment
298298
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:545:12
@@ -342,17 +342,137 @@ LL | const NO_SAFETY_IN_IMPL: i32 = unsafe { 1 };
342342
|
343343
= help: consider adding a safety comment on the preceding line
344344

345+
error: constant has unnecessary safety comment
346+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:701:5
347+
|
348+
LL | const UNIX_EPOCH_JULIAN_DAY: i32 =
349+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
350+
|
351+
help: consider removing the safety comment
352+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:699:8
353+
|
354+
LL | // SAFETY: fail ONLY if `accept-comment-above-attribute = false`
355+
| ^^^^^^^
356+
345357
error: statement has unnecessary safety comment
346-
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:719:5
358+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:721:5
347359
|
348360
LL | _ = bar();
349361
| ^^^^^^^^^^
350362
|
351363
help: consider removing the safety comment
352-
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:718:5
364+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:720:8
353365
|
354366
LL | // SAFETY: unnecessary_safety_comment triggers here
355-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
367+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
368+
369+
error: module has unnecessary safety comment
370+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:741:5
371+
|
372+
LL | mod x {}
373+
| ^^^^^^^^
374+
|
375+
help: consider removing the safety comment
376+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:740:8
377+
|
378+
LL | // SAFETY: ...
379+
| ^^^^^^^
380+
381+
error: module has unnecessary safety comment
382+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:746:5
383+
|
384+
LL | mod y {}
385+
| ^^^^^^^^
386+
|
387+
help: consider removing the safety comment
388+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:744:8
389+
|
390+
LL | // SAFETY: ...
391+
| ^^^^^^^
392+
393+
error: module has unnecessary safety comment
394+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:751:5
395+
|
396+
LL | mod z {}
397+
| ^^^^^^^^
398+
|
399+
help: consider removing the safety comment
400+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:750:8
401+
|
402+
LL | // SAFETY: ...
403+
| ^^^^^^^
404+
405+
error: module has unnecessary safety comment
406+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:759:5
407+
|
408+
LL | mod y {}
409+
| ^^^^^^^^
410+
|
411+
help: consider removing the safety comment
412+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:757:8
413+
|
414+
LL | // SAFETY: ...
415+
| ^^^^^^^
416+
417+
error: statement has unnecessary safety comment
418+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:774:9
419+
|
420+
LL | let x = 34;
421+
| ^^^^^^^^^^^
422+
|
423+
help: consider removing the safety comment
424+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:772:12
425+
|
426+
LL | // SAFETY: ...
427+
| ^^^^^^^^^^^
428+
429+
error: function has unnecessary safety comment
430+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:781:5
431+
|
432+
LL | unsafe fn unsafe_comment() {}
433+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
434+
|
435+
help: consider changing the `safety` comment for a `# Safety` doc comment
436+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:780:8
437+
|
438+
LL | // SAFETY: Bla
439+
| ^^^^^^^
440+
441+
error: function has unnecessary safety comment
442+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:787:5
443+
|
444+
LL | unsafe fn unsafe_block_comment() {}
445+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
446+
|
447+
help: consider changing the `safety` comment for a `# Safety` doc comment
448+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:785:8
449+
|
450+
LL | SAFETY: Bla
451+
| ^^^^^^^
452+
453+
error: function has unnecessary safety comment
454+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:791:5
455+
|
456+
LL | fn safe_comment() {}
457+
| ^^^^^^^^^^^^^^^^^^^^
458+
|
459+
help: consider removing the safety comment
460+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:790:8
461+
|
462+
LL | // SAFETY: Bla
463+
| ^^^^^^^
464+
465+
error: function has unnecessary safety comment
466+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:795:5
467+
|
468+
LL | fn safe_doc_comment() {}
469+
| ^^^^^^^^^^^^^^^^^^^^^^^^
470+
|
471+
help: consider removing the safety comment
472+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:794:9
473+
|
474+
LL | /// SAFETY: Bla
475+
| ^^^^^^^
356476

357-
error: aborting due to 40 previous errors
477+
error: aborting due to 50 previous errors
358478

tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.disabled.stderr

Lines changed: 89 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -247,10 +247,10 @@ LL | const BIG_NUMBER: i32 = 1000000;
247247
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
248248
|
249249
help: consider removing the safety comment
250-
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:507:5
250+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:507:8
251251
|
252252
LL | // SAFETY:
253-
| ^^^^^^^^^^
253+
| ^^^^^^^
254254
= note: `-D clippy::unnecessary-safety-comment` implied by `-D warnings`
255255
= help: to override `-D warnings` add `#[allow(clippy::unnecessary_safety_comment)]`
256256

@@ -297,10 +297,10 @@ LL | | };
297297
| |______^
298298
|
299299
help: consider removing the safety comment
300-
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:542:5
300+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:542:8
301301
|
302302
LL | // SAFETY: this is more than one level away, so it should warn
303-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
303+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
304304

305305
error: unsafe block missing a safety comment
306306
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:545:12
@@ -439,24 +439,104 @@ LL | unsafe { Date::__from_ordinal_date_unchecked(1970, 1) }.into_julian
439439
= help: consider adding a safety comment on the preceding line
440440

441441
error: statement has unnecessary safety comment
442-
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:719:5
442+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:721:5
443443
|
444444
LL | _ = bar();
445445
| ^^^^^^^^^^
446446
|
447447
help: consider removing the safety comment
448-
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:718:5
448+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:720:8
449449
|
450450
LL | // SAFETY: unnecessary_safety_comment triggers here
451-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
451+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
452452

453453
error: unsafe block missing a safety comment
454-
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:733:12
454+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:735:12
455455
|
456456
LL | return unsafe { h() };
457457
| ^^^^^^^^^^^^^^
458458
|
459459
= help: consider adding a safety comment on the preceding line
460460

461-
error: aborting due to 53 previous errors
461+
error: module has unnecessary safety comment
462+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:741:5
463+
|
464+
LL | mod x {}
465+
| ^^^^^^^^
466+
|
467+
help: consider removing the safety comment
468+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:740:8
469+
|
470+
LL | // SAFETY: ...
471+
| ^^^^^^^
472+
473+
error: module has unnecessary safety comment
474+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:751:5
475+
|
476+
LL | mod z {}
477+
| ^^^^^^^^
478+
|
479+
help: consider removing the safety comment
480+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:750:8
481+
|
482+
LL | // SAFETY: ...
483+
| ^^^^^^^
484+
485+
error: unsafe block missing a safety comment
486+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:766:9
487+
|
488+
LL | unsafe {}
489+
| ^^^^^^^^^
490+
|
491+
= help: consider adding a safety comment on the preceding line
492+
493+
error: function has unnecessary safety comment
494+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:781:5
495+
|
496+
LL | unsafe fn unsafe_comment() {}
497+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
498+
|
499+
help: consider changing the `safety` comment for a `# Safety` doc comment
500+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:780:8
501+
|
502+
LL | // SAFETY: Bla
503+
| ^^^^^^^
504+
505+
error: function has unnecessary safety comment
506+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:787:5
507+
|
508+
LL | unsafe fn unsafe_block_comment() {}
509+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
510+
|
511+
help: consider changing the `safety` comment for a `# Safety` doc comment
512+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:785:8
513+
|
514+
LL | SAFETY: Bla
515+
| ^^^^^^^
516+
517+
error: function has unnecessary safety comment
518+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:791:5
519+
|
520+
LL | fn safe_comment() {}
521+
| ^^^^^^^^^^^^^^^^^^^^
522+
|
523+
help: consider removing the safety comment
524+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:790:8
525+
|
526+
LL | // SAFETY: Bla
527+
| ^^^^^^^
528+
529+
error: function has unnecessary safety comment
530+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:795:5
531+
|
532+
LL | fn safe_doc_comment() {}
533+
| ^^^^^^^^^^^^^^^^^^^^^^^^
534+
|
535+
help: consider removing the safety comment
536+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:794:9
537+
|
538+
LL | /// SAFETY: Bla
539+
| ^^^^^^^
540+
541+
error: aborting due to 60 previous errors
462542

0 commit comments

Comments
 (0)