Skip to content

Commit 2edabe8

Browse files
committed
fix: unnecessary_safety_comment does not lint for the first line
1 parent 0397819 commit 2edabe8

File tree

4 files changed

+51
-8
lines changed

4 files changed

+51
-8
lines changed

clippy_lints/src/undocumented_unsafe_blocks.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ fn item_has_safety_comment(cx: &LateContext<'_>, item: &hir::Item<'_>) -> HasSaf
505505
},
506506
Node::Stmt(stmt) => {
507507
if let Node::Block(block) = cx.tcx.parent_hir_node(stmt.hir_id) {
508-
walk_span_to_context(block.span, SyntaxContext::root()).map(Span::lo)
508+
walk_span_to_context(block.span, SyntaxContext::root()).map(|sp| (sp.lo(), false))
509509
} else {
510510
// Problem getting the parent node. Pretend a comment was found.
511511
return HasSafetyComment::Maybe;
@@ -518,18 +518,20 @@ fn item_has_safety_comment(cx: &LateContext<'_>, item: &hir::Item<'_>) -> HasSaf
518518
};
519519

520520
let source_map = cx.sess().source_map();
521-
if let Some(comment_start) = comment_start
521+
// If the comment is in the first line of the file, there is no preceding line
522+
if let Some((comment_start, include_first_line_of_file)) = comment_start
522523
&& let Ok(unsafe_line) = source_map.lookup_line(item.span.lo())
523524
&& let Ok(comment_start_line) = source_map.lookup_line(comment_start)
524-
&& Arc::ptr_eq(&unsafe_line.sf, &comment_start_line.sf)
525+
&& (include_first_line_of_file || Arc::ptr_eq(&unsafe_line.sf, &comment_start_line.sf))
525526
&& let Some(src) = unsafe_line.sf.src.as_deref()
526527
{
527528
return if comment_start_line.line >= unsafe_line.line {
528529
HasSafetyComment::No
529530
} else {
530531
match text_has_safety_comment(
531532
src,
532-
&unsafe_line.sf.lines()[comment_start_line.line + 1..=unsafe_line.line],
533+
&unsafe_line.sf.lines()
534+
[(comment_start_line.line + usize::from(!include_first_line_of_file))..=unsafe_line.line],
533535
unsafe_line.sf.start_pos,
534536
) {
535537
Some(b) => HasSafetyComment::Yes(b),
@@ -597,23 +599,23 @@ fn comment_start_before_item_in_mod(
597599
parent_mod: &hir::Mod<'_>,
598600
parent_mod_span: Span,
599601
item: &hir::Item<'_>,
600-
) -> Option<BytePos> {
602+
) -> Option<(BytePos, bool)> {
601603
parent_mod.item_ids.iter().enumerate().find_map(|(idx, item_id)| {
602604
if *item_id == item.item_id() {
603605
if idx == 0 {
604606
// mod A { /* comment */ unsafe impl T {} ... }
605607
// ^------------------------------------------^ returns the start of this span
606608
// ^---------------------^ finally checks comments in this range
607609
if let Some(sp) = walk_span_to_context(parent_mod_span, SyntaxContext::root()) {
608-
return Some(sp.lo());
610+
return Some((sp.lo(), false));
609611
}
610612
} else {
611613
// some_item /* comment */ unsafe impl T {}
612614
// ^-------^ returns the end of this span
613615
// ^---------------^ finally checks comments in this range
614616
let prev_item = cx.tcx.hir_item(parent_mod.item_ids[idx - 1]);
615617
if let Some(sp) = walk_span_to_context(prev_item.span, SyntaxContext::root()) {
616-
return Some(sp.hi());
618+
return Some((sp.hi(), sp.is_dummy()));
617619
}
618620
}
619621
}
@@ -668,7 +670,7 @@ fn get_body_search_span(cx: &LateContext<'_>) -> Option<Span> {
668670
}) => {
669671
return maybe_mod_item
670672
.and_then(|item| comment_start_before_item_in_mod(cx, mod_, *span, &item))
671-
.map(|comment_start| mod_.spans.inner_span.with_lo(comment_start))
673+
.map(|(comment_start, _)| mod_.spans.inner_span.with_lo(comment_start))
672674
.or(Some(*span));
673675
},
674676
node if let Some((span, _)) = span_and_hid_of_item_alike_node(&node)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error: module has unnecessary safety comment
2+
--> src/main.rs:2:1
3+
|
4+
2 | mod x {}
5+
| ^^^^^^^^
6+
|
7+
help: consider removing the safety comment
8+
--> src/main.rs:1:1
9+
|
10+
1 | // SAFETY: ...
11+
| ^^^^^^^^^^^^^^
12+
= note: requested on the command line with `-D clippy::unnecessary-safety-comment`
13+
14+
error: module has unnecessary safety comment
15+
--> src/main.rs:5:1
16+
|
17+
5 | mod y {}
18+
| ^^^^^^^^
19+
|
20+
help: consider removing the safety comment
21+
--> src/main.rs:4:1
22+
|
23+
4 | // SAFETY: ...
24+
| ^^^^^^^^^^^^^^
25+
26+
error: could not compile `undocumented_unsafe_blocks` (bin "undocumented_unsafe_blocks") due to 2 previous errors
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "undocumented_unsafe_blocks"
3+
edition = "2024"
4+
publish = false
5+
version = "0.1.0"
6+
7+
[lints.clippy]
8+
unnecessary_safety_comment = "deny"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// SAFETY: ...
2+
mod x {}
3+
4+
// SAFETY: ...
5+
mod y {}
6+
7+
fn main() {}

0 commit comments

Comments
 (0)