Skip to content

Commit f3fb1a3

Browse files
committed
Considering comments above attributes for items
1 parent 4f07fe2 commit f3fb1a3

File tree

4 files changed

+99
-12
lines changed

4 files changed

+99
-12
lines changed

clippy_lints/src/undocumented_unsafe_blocks.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ impl<'tcx> LateLintPass<'tcx> for UndocumentedUnsafeBlocks {
201201
(span, help_span)
202202
};
203203

204-
let item_has_safety_comment = item_has_safety_comment(cx, item);
204+
let item_has_safety_comment = item_has_safety_comment(cx, item, self.accept_comment_above_attributes);
205205
match item_has_safety_comment {
206206
HasSafetyComment::Yes(pos) => check_has_safety_comment(cx, item, mk_spans(pos)),
207207
HasSafetyComment::No => check_has_no_safety_comment(cx, item),
@@ -272,6 +272,7 @@ fn check_has_safety_comment(cx: &LateContext<'_>, item: &hir::Item<'_>, (span, h
272272
},
273273
}
274274
}
275+
275276
fn check_has_no_safety_comment(cx: &LateContext<'_>, item: &hir::Item<'_>) {
276277
if let ItemKind::Impl(Impl {
277278
of_trait: Some(of_trait),
@@ -482,6 +483,7 @@ fn include_attrs_in_span(cx: &LateContext<'_>, hir_id: HirId, span: Span) -> Spa
482483
}))
483484
}
484485

486+
#[derive(Debug)]
485487
enum HasSafetyComment {
486488
Yes(BytePos),
487489
No,
@@ -490,7 +492,11 @@ enum HasSafetyComment {
490492

491493
/// Checks if the lines immediately preceding the item contain a safety comment.
492494
#[allow(clippy::collapsible_match)]
493-
fn item_has_safety_comment(cx: &LateContext<'_>, item: &hir::Item<'_>) -> HasSafetyComment {
495+
fn item_has_safety_comment(
496+
cx: &LateContext<'_>,
497+
item: &hir::Item<'_>,
498+
accept_comment_above_attributes: bool,
499+
) -> HasSafetyComment {
494500
match span_from_macro_expansion_has_safety_comment(cx, item.span) {
495501
HasSafetyComment::Maybe => (),
496502
has_safety_comment => return has_safety_comment,
@@ -524,10 +530,15 @@ fn item_has_safety_comment(cx: &LateContext<'_>, item: &hir::Item<'_>) -> HasSaf
524530
},
525531
};
526532

533+
let mut span = item.span;
534+
if accept_comment_above_attributes {
535+
span = include_attrs_in_span(cx, item.hir_id(), span);
536+
}
537+
527538
let source_map = cx.sess().source_map();
528539
// If the comment is in the first line of the file, there is no preceding line
529540
if let Some(comment_start) = comment_start
530-
&& let Ok(unsafe_line) = source_map.lookup_line(item.span.lo())
541+
&& let Ok(unsafe_line) = source_map.lookup_line(span.lo())
531542
&& let Ok(comment_start_line) = source_map.lookup_line(comment_start.into())
532543
&& let include_first_line_of_file = matches!(comment_start, CommentStartBeforeItem::Start)
533544
&& (include_first_line_of_file || Arc::ptr_eq(&unsafe_line.sf, &comment_start_line.sf))
@@ -572,8 +583,6 @@ fn stmt_has_safety_comment(
572583
_ => return HasSafetyComment::Maybe,
573584
};
574585

575-
// if span_with_attrs_has_safety_comment(cx, span, hir_id, accept_comment_above_attrib
576-
// }
577586
let mut span = span;
578587
if accept_comment_above_attributes {
579588
span = include_attrs_in_span(cx, hir_id, span);
@@ -586,6 +595,7 @@ fn stmt_has_safety_comment(
586595
&& Arc::ptr_eq(&unsafe_line.sf, &comment_start_line.sf)
587596
&& let Some(src) = unsafe_line.sf.src.as_deref()
588597
{
598+
// dbg!(src);
589599
return if comment_start_line.line >= unsafe_line.line {
590600
HasSafetyComment::No
591601
} else {

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

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,17 +342,53 @@ 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:5
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:720: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:719:5
353365
|
354366
LL | // SAFETY: unnecessary_safety_comment triggers here
355367
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
356368

357-
error: aborting due to 40 previous errors
369+
error: module has unnecessary safety comment
370+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:740: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:739:5
377+
|
378+
LL | // SAFETY: ...
379+
| ^^^^^^^^^^^^^^
380+
381+
error: module has unnecessary safety comment
382+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:745: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:743:5
389+
|
390+
LL | // SAFETY: ...
391+
| ^^^^^^^^^^^^^^
392+
393+
error: aborting due to 43 previous errors
358394

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

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -439,24 +439,48 @@ 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:720: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:719:5
449449
|
450450
LL | // SAFETY: unnecessary_safety_comment triggers here
451451
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
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:734: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:740: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:739:5
469+
|
470+
LL | // SAFETY: ...
471+
| ^^^^^^^^^^^^^^
472+
473+
error: module has unnecessary safety comment
474+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:750: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:749:5
481+
|
482+
LL | // SAFETY: ...
483+
| ^^^^^^^^^^^^^^
484+
485+
error: aborting due to 55 previous errors
462486

tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,7 @@ mod issue_11709_regression {
701701
const UNIX_EPOCH_JULIAN_DAY: i32 =
702702
unsafe { Date::__from_ordinal_date_unchecked(1970, 1) }.into_julian_day_just_make_this_line_longer();
703703
//~[disabled]^ undocumented_unsafe_blocks
704+
//~[default]^^^ unnecessary_safety_comment
704705
}
705706

706707
fn issue_13039() {
@@ -734,4 +735,20 @@ fn rfl_issue15034() -> i32 {
734735
//~[disabled]^ ERROR: unsafe block missing a safety comment
735736
}
736737

738+
mod issue_14555 {
739+
// SAFETY: ...
740+
mod x {}
741+
//~^ unnecessary_safety_comment
742+
743+
// SAFETY: ...
744+
#[doc(hidden)]
745+
mod y {}
746+
//~[default]^ unnecessary_safety_comment
747+
748+
#[doc(hidden)]
749+
// SAFETY: ...
750+
mod z {}
751+
//~[disabled]^ unnecessary_safety_comment
752+
}
753+
737754
fn main() {}

0 commit comments

Comments
 (0)