Skip to content

Commit 9c69e1c

Browse files
committed
Simplify
1 parent 4fa5757 commit 9c69e1c

File tree

1 file changed

+78
-95
lines changed

1 file changed

+78
-95
lines changed

clippy_lints/src/undocumented_unsafe_blocks.rs

Lines changed: 78 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -363,72 +363,60 @@ fn item_has_safety_comment(cx: &LateContext<'_>, item: &hir::Item<'_>) -> HasSaf
363363
has_safety_comment => return has_safety_comment,
364364
}
365365

366-
if item.span.ctxt() == SyntaxContext::root() {
367-
if let Some(parent_node) = get_parent_node(cx.tcx, item.hir_id()) {
368-
let comment_start = match parent_node {
369-
Node::Crate(parent_mod) => {
370-
comment_start_before_item_in_mod(cx, parent_mod, parent_mod.spans.inner_span, item)
371-
},
372-
Node::Item(parent_item) => {
373-
if let ItemKind::Mod(parent_mod) = &parent_item.kind {
374-
comment_start_before_item_in_mod(cx, parent_mod, parent_item.span, item)
375-
} else {
376-
// Doesn't support impls in this position. Pretend a comment was found.
377-
return HasSafetyComment::Maybe;
378-
}
379-
},
380-
Node::Stmt(stmt) => {
381-
if let Some(stmt_parent) = get_parent_node(cx.tcx, stmt.hir_id) {
382-
match stmt_parent {
383-
Node::Block(block) => walk_span_to_context(block.span, SyntaxContext::root()).map(Span::lo),
384-
_ => {
385-
// Doesn't support impls in this position. Pretend a comment was found.
386-
return HasSafetyComment::Maybe;
387-
},
388-
}
389-
} else {
390-
// Problem getting the parent node. Pretend a comment was found.
391-
return HasSafetyComment::Maybe;
392-
}
393-
},
394-
_ => {
366+
if item.span.ctxt() != SyntaxContext::root() {
367+
return HasSafetyComment::No;
368+
}
369+
if let Some(parent_node) = get_parent_node(cx.tcx, item.hir_id()) {
370+
let comment_start = match parent_node {
371+
Node::Crate(parent_mod) => {
372+
comment_start_before_item_in_mod(cx, parent_mod, parent_mod.spans.inner_span, item)
373+
},
374+
Node::Item(parent_item) => {
375+
if let ItemKind::Mod(parent_mod) = &parent_item.kind {
376+
comment_start_before_item_in_mod(cx, parent_mod, parent_item.span, item)
377+
} else {
395378
// Doesn't support impls in this position. Pretend a comment was found.
396379
return HasSafetyComment::Maybe;
397-
},
398-
};
380+
}
381+
},
382+
Node::Stmt(stmt) => {
383+
if let Some(Node::Block(block)) = get_parent_node(cx.tcx, stmt.hir_id) {
384+
walk_span_to_context(block.span, SyntaxContext::root()).map(Span::lo)
385+
} else {
386+
// Problem getting the parent node. Pretend a comment was found.
387+
return HasSafetyComment::Maybe;
388+
}
389+
},
390+
_ => {
391+
// Doesn't support impls in this position. Pretend a comment was found.
392+
return HasSafetyComment::Maybe;
393+
},
394+
};
399395

400-
let source_map = cx.sess().source_map();
401-
if let Some(comment_start) = comment_start
402-
&& let Ok(unsafe_line) = source_map.lookup_line(item.span.lo())
403-
&& let Ok(comment_start_line) = source_map.lookup_line(comment_start)
404-
&& Lrc::ptr_eq(&unsafe_line.sf, &comment_start_line.sf)
405-
&& let Some(src) = unsafe_line.sf.src.as_deref()
406-
{
407-
unsafe_line.sf.lines(|lines| {
408-
if comment_start_line.line >= unsafe_line.line {
409-
HasSafetyComment::No
410-
} else {
411-
match text_has_safety_comment(
412-
src,
413-
&lines[comment_start_line.line + 1..=unsafe_line.line],
414-
unsafe_line.sf.start_pos.to_usize(),
415-
) {
416-
Some(b) => HasSafetyComment::Yes(b),
417-
None => HasSafetyComment::No,
418-
}
396+
let source_map = cx.sess().source_map();
397+
if let Some(comment_start) = comment_start
398+
&& let Ok(unsafe_line) = source_map.lookup_line(item.span.lo())
399+
&& let Ok(comment_start_line) = source_map.lookup_line(comment_start)
400+
&& Lrc::ptr_eq(&unsafe_line.sf, &comment_start_line.sf)
401+
&& let Some(src) = unsafe_line.sf.src.as_deref()
402+
{
403+
return unsafe_line.sf.lines(|lines| {
404+
if comment_start_line.line >= unsafe_line.line {
405+
HasSafetyComment::No
406+
} else {
407+
match text_has_safety_comment(
408+
src,
409+
&lines[comment_start_line.line + 1..=unsafe_line.line],
410+
unsafe_line.sf.start_pos.to_usize(),
411+
) {
412+
Some(b) => HasSafetyComment::Yes(b),
413+
None => HasSafetyComment::No,
419414
}
420-
})
421-
} else {
422-
// Problem getting source text. Pretend a comment was found.
423-
HasSafetyComment::Maybe
424-
}
425-
} else {
426-
// No parent node. Pretend a comment was found.
427-
HasSafetyComment::Maybe
415+
}
416+
});
428417
}
429-
} else {
430-
HasSafetyComment::No
431418
}
419+
HasSafetyComment::Maybe
432420
}
433421

434422
/// Checks if the lines immediately preceding the item contain a safety comment.
@@ -439,45 +427,40 @@ fn stmt_has_safety_comment(cx: &LateContext<'_>, span: Span, hir_id: HirId) -> H
439427
has_safety_comment => return has_safety_comment,
440428
}
441429

442-
if span.ctxt() == SyntaxContext::root() {
443-
if let Some(parent_node) = get_parent_node(cx.tcx, hir_id) {
444-
let comment_start = match parent_node {
445-
Node::Block(block) => walk_span_to_context(block.span, SyntaxContext::root()).map(Span::lo),
446-
_ => return HasSafetyComment::Maybe,
447-
};
430+
if span.ctxt() != SyntaxContext::root() {
431+
return HasSafetyComment::No;
432+
}
448433

449-
let source_map = cx.sess().source_map();
450-
if let Some(comment_start) = comment_start
451-
&& let Ok(unsafe_line) = source_map.lookup_line(span.lo())
452-
&& let Ok(comment_start_line) = source_map.lookup_line(comment_start)
453-
&& Lrc::ptr_eq(&unsafe_line.sf, &comment_start_line.sf)
454-
&& let Some(src) = unsafe_line.sf.src.as_deref()
455-
{
456-
unsafe_line.sf.lines(|lines| {
457-
if comment_start_line.line >= unsafe_line.line {
458-
HasSafetyComment::No
459-
} else {
460-
match text_has_safety_comment(
461-
src,
462-
&lines[comment_start_line.line + 1..=unsafe_line.line],
463-
unsafe_line.sf.start_pos.to_usize(),
464-
) {
465-
Some(b) => HasSafetyComment::Yes(b),
466-
None => HasSafetyComment::No,
467-
}
434+
if let Some(parent_node) = get_parent_node(cx.tcx, hir_id) {
435+
let comment_start = match parent_node {
436+
Node::Block(block) => walk_span_to_context(block.span, SyntaxContext::root()).map(Span::lo),
437+
_ => return HasSafetyComment::Maybe,
438+
};
439+
440+
let source_map = cx.sess().source_map();
441+
if let Some(comment_start) = comment_start
442+
&& let Ok(unsafe_line) = source_map.lookup_line(span.lo())
443+
&& let Ok(comment_start_line) = source_map.lookup_line(comment_start)
444+
&& Lrc::ptr_eq(&unsafe_line.sf, &comment_start_line.sf)
445+
&& let Some(src) = unsafe_line.sf.src.as_deref()
446+
{
447+
return unsafe_line.sf.lines(|lines| {
448+
if comment_start_line.line >= unsafe_line.line {
449+
HasSafetyComment::No
450+
} else {
451+
match text_has_safety_comment(
452+
src,
453+
&lines[comment_start_line.line + 1..=unsafe_line.line],
454+
unsafe_line.sf.start_pos.to_usize(),
455+
) {
456+
Some(b) => HasSafetyComment::Yes(b),
457+
None => HasSafetyComment::No,
468458
}
469-
})
470-
} else {
471-
// Problem getting source text. Pretend a comment was found.
472-
HasSafetyComment::Maybe
473-
}
474-
} else {
475-
// No parent node. Pretend a comment was found.
476-
HasSafetyComment::Maybe
459+
}
460+
});
477461
}
478-
} else {
479-
HasSafetyComment::No
480462
}
463+
HasSafetyComment::Maybe
481464
}
482465

483466
fn comment_start_before_item_in_mod(

0 commit comments

Comments
 (0)