Skip to content

Commit 834a770

Browse files
committed
Tmp
1 parent f4c6ab0 commit 834a770

File tree

1 file changed

+8
-30
lines changed

1 file changed

+8
-30
lines changed

clippy_lints/src/undocumented_unsafe_blocks.rs

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -505,8 +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())
509-
.map(|sp| CommentStartBeforeItem::Offset(sp.lo()))
508+
walk_span_to_context(block.span, SyntaxContext::root()).map(Span::lo)
510509
} else {
511510
// Problem getting the parent node. Pretend a comment was found.
512511
return HasSafetyComment::Maybe;
@@ -519,21 +518,18 @@ fn item_has_safety_comment(cx: &LateContext<'_>, item: &hir::Item<'_>) -> HasSaf
519518
};
520519

521520
let source_map = cx.sess().source_map();
522-
// If the comment is in the first line of the file, there is no preceding line
523521
if let Some(comment_start) = comment_start
524522
&& let Ok(unsafe_line) = source_map.lookup_line(item.span.lo())
525-
&& let Ok(comment_start_line) = source_map.lookup_line(comment_start.into())
526-
&& let include_first_line_of_file = matches!(comment_start, CommentStartBeforeItem::Start)
527-
&& (include_first_line_of_file || Arc::ptr_eq(&unsafe_line.sf, &comment_start_line.sf))
523+
&& let Ok(comment_start_line) = source_map.lookup_line(comment_start)
524+
&& Arc::ptr_eq(&unsafe_line.sf, &comment_start_line.sf)
528525
&& let Some(src) = unsafe_line.sf.src.as_deref()
529526
{
530527
return if comment_start_line.line >= unsafe_line.line {
531528
HasSafetyComment::No
532529
} else {
533530
match text_has_safety_comment(
534531
src,
535-
&unsafe_line.sf.lines()
536-
[(comment_start_line.line + usize::from(!include_first_line_of_file))..=unsafe_line.line],
532+
&unsafe_line.sf.lines()[comment_start_line.line + 1..=unsafe_line.line],
537533
unsafe_line.sf.start_pos,
538534
) {
539535
Some(b) => HasSafetyComment::Yes(b),
@@ -596,46 +592,28 @@ fn stmt_has_safety_comment(
596592
HasSafetyComment::Maybe
597593
}
598594

599-
#[derive(Clone, Copy, Debug)]
600-
enum CommentStartBeforeItem {
601-
Offset(BytePos),
602-
Start,
603-
}
604-
605-
impl From<CommentStartBeforeItem> for BytePos {
606-
fn from(value: CommentStartBeforeItem) -> Self {
607-
match value {
608-
CommentStartBeforeItem::Offset(loc) => loc,
609-
CommentStartBeforeItem::Start => BytePos(0),
610-
}
611-
}
612-
}
613-
614595
fn comment_start_before_item_in_mod(
615596
cx: &LateContext<'_>,
616597
parent_mod: &hir::Mod<'_>,
617598
parent_mod_span: Span,
618599
item: &hir::Item<'_>,
619-
) -> Option<CommentStartBeforeItem> {
600+
) -> Option<BytePos> {
620601
parent_mod.item_ids.iter().enumerate().find_map(|(idx, item_id)| {
621602
if *item_id == item.item_id() {
622603
if idx == 0 {
623604
// mod A { /* comment */ unsafe impl T {} ... }
624605
// ^------------------------------------------^ returns the start of this span
625606
// ^---------------------^ finally checks comments in this range
626607
if let Some(sp) = walk_span_to_context(parent_mod_span, SyntaxContext::root()) {
627-
return Some(CommentStartBeforeItem::Offset(sp.lo()));
608+
return Some(sp.lo());
628609
}
629610
} else {
630611
// some_item /* comment */ unsafe impl T {}
631612
// ^-------^ returns the end of this span
632613
// ^---------------^ finally checks comments in this range
633614
let prev_item = cx.tcx.hir_item(parent_mod.item_ids[idx - 1]);
634-
if prev_item.span.is_dummy() {
635-
return Some(CommentStartBeforeItem::Start);
636-
}
637615
if let Some(sp) = walk_span_to_context(prev_item.span, SyntaxContext::root()) {
638-
return Some(CommentStartBeforeItem::Offset(sp.hi()));
616+
return Some(sp.hi());
639617
}
640618
}
641619
}
@@ -690,7 +668,7 @@ fn get_body_search_span(cx: &LateContext<'_>) -> Option<Span> {
690668
}) => {
691669
return maybe_mod_item
692670
.and_then(|item| comment_start_before_item_in_mod(cx, mod_, *span, &item))
693-
.map(|comment_start| mod_.spans.inner_span.with_lo(comment_start.into()))
671+
.map(|comment_start| mod_.spans.inner_span.with_lo(comment_start))
694672
.or(Some(*span));
695673
},
696674
node if let Some((span, _)) = span_and_hid_of_item_alike_node(&node)

0 commit comments

Comments
 (0)