Skip to content

Commit

Permalink
refac: remove procedures for linting inside macros
Browse files Browse the repository at this point in the history
  • Loading branch information
koka831 committed Jan 7, 2023
1 parent ac57ec5 commit db6e337
Showing 1 changed file with 1 addition and 51 deletions.
52 changes: 1 addition & 51 deletions clippy_lints/src/undocumented_unsafe_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,11 +340,7 @@ fn block_has_safety_comment(cx: &LateContext<'_>, span: Span) -> bool {
// ```
// won't work. This is to avoid dealing with where such a comment should be place relative to
// attributes and doc comments.

matches!(
span_from_macro_expansion_has_safety_comment(cx, span),
HasSafetyComment::Yes(_)
) || span_in_body_has_safety_comment(cx, span)
span_in_body_has_safety_comment(cx, span)
}

enum HasSafetyComment {
Expand All @@ -356,11 +352,6 @@ enum HasSafetyComment {
/// Checks if the lines immediately preceding the item contain a safety comment.
#[allow(clippy::collapsible_match)]
fn item_has_safety_comment(cx: &LateContext<'_>, item: &hir::Item<'_>) -> HasSafetyComment {
match span_from_macro_expansion_has_safety_comment(cx, item.span) {
HasSafetyComment::Maybe => (),
has_safety_comment => return has_safety_comment,
}

if item.span.ctxt() != SyntaxContext::root() {
return HasSafetyComment::No;
}
Expand Down Expand Up @@ -420,11 +411,6 @@ fn item_has_safety_comment(cx: &LateContext<'_>, item: &hir::Item<'_>) -> HasSaf
/// Checks if the lines immediately preceding the item contain a safety comment.
#[allow(clippy::collapsible_match)]
fn stmt_has_safety_comment(cx: &LateContext<'_>, span: Span, hir_id: HirId) -> HasSafetyComment {
match span_from_macro_expansion_has_safety_comment(cx, span) {
HasSafetyComment::Maybe => (),
has_safety_comment => return has_safety_comment,
}

if span.ctxt() != SyntaxContext::root() {
return HasSafetyComment::No;
}
Expand Down Expand Up @@ -490,42 +476,6 @@ fn comment_start_before_item_in_mod(
})
}

fn span_from_macro_expansion_has_safety_comment(cx: &LateContext<'_>, span: Span) -> HasSafetyComment {
let source_map = cx.sess().source_map();
let ctxt = span.ctxt();
if ctxt == SyntaxContext::root() {
HasSafetyComment::Maybe
} else {
// From a macro expansion. Get the text from the start of the macro declaration to start of the
// unsafe block.
// macro_rules! foo { () => { stuff }; (x) => { unsafe { stuff } }; }
// ^--------------------------------------------^
if let Ok(unsafe_line) = source_map.lookup_line(span.lo())
&& let Ok(macro_line) = source_map.lookup_line(ctxt.outer_expn_data().def_site.lo())
&& Lrc::ptr_eq(&unsafe_line.sf, &macro_line.sf)
&& let Some(src) = unsafe_line.sf.src.as_deref()
{
unsafe_line.sf.lines(|lines| {
if macro_line.line < unsafe_line.line {
match text_has_safety_comment(
src,
&lines[macro_line.line + 1..=unsafe_line.line],
unsafe_line.sf.start_pos.to_usize(),
) {
Some(b) => HasSafetyComment::Yes(b),
None => HasSafetyComment::No,
}
} else {
HasSafetyComment::No
}
})
} else {
// Problem getting source text. Pretend a comment was found.
HasSafetyComment::Maybe
}
}
}

fn get_body_search_span(cx: &LateContext<'_>) -> Option<Span> {
let body = cx.enclosing_body?;
let map = cx.tcx.hir();
Expand Down

0 comments on commit db6e337

Please sign in to comment.