Skip to content

Commit

Permalink
clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
ebobrow committed Jun 16, 2021
1 parent d194de2 commit 0fdcaa8
Showing 1 changed file with 21 additions and 41 deletions.
62 changes: 21 additions & 41 deletions clippy_lints/src/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,18 +480,21 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
errors: false,
panics: false,
};
let mut in_code = false;
let mut in_code_block = false;
let mut in_link = None;
let mut in_heading = false;
let mut is_rust = false;
let mut edition = None;
let mut ticks_inverted = false;
let mut prev_was_ticks: Option<Span> = None;
let mut prev_was_code: Option<Span> = None;
for (event, range) in events {
let in_ticks = matches!(event, Code(_));
let in_code = matches!(event, Code(_));
if !matches!(event, FootnoteReference(_) | Text(_) | Code(_)) {
prev_was_code = None;
}
match event {
Start(CodeBlock(ref kind)) => {
in_code = true;
in_code_block = true;
if let CodeBlockKind::Fenced(lang) = kind {
for item in lang.split(',') {
if item == "ignore" {
Expand All @@ -506,50 +509,28 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
}
}
}
prev_was_ticks = None;
},
End(CodeBlock(_)) => {
in_code = false;
in_code_block = false;
is_rust = false;
prev_was_ticks = None;
},
Start(Link(_, url, _)) => {
prev_was_ticks = None;
in_link = Some(url);
},
End(Link(..)) => {
prev_was_ticks = None;
in_link = None;
},
Start(Heading(_)) => {
prev_was_ticks = None;
in_heading = true;
},
End(Heading(_)) => {
prev_was_ticks = None;
in_heading = false;
},
Start(_tag) | End(_tag) => {
// We don't care about other tags
prev_was_ticks = None;
},
Html(_html) => {
// HTML is weird, just ignore it
prev_was_ticks = None;
},
SoftBreak | HardBreak | TaskListMarker(_) | Rule => {
prev_was_ticks = None;
},
Start(Link(_, url, _)) => in_link = Some(url),
End(Link(..)) => in_link = None,
Start(Heading(_)) => in_heading = true,
End(Heading(_)) => in_heading = false,
Start(_tag) | End(_tag) => (), // We don't care about other tags
Html(_html) => (), // HTML is weird, just ignore it
SoftBreak | HardBreak | TaskListMarker(_) | Rule => (),
FootnoteReference(text) | Text(text) | Code(text) => {
let index = match spans.binary_search_by(|c| c.0.cmp(&range.start)) {
Ok(o) => o,
Err(e) => e - 1,
};
let (begin, span) = spans[index];
if ticks_inverted && !in_ticks || !ticks_inverted && in_ticks {
if ticks_inverted && !in_code || !ticks_inverted && in_code {
let span = span.with_lo(span.lo() + BytePos::from_usize(range.start - begin));
let span = span.with_hi(span.lo() + BytePos::from_usize(1));
prev_was_ticks = Some(span);
prev_was_code = Some(span);
} else {
if Some(&text) == in_link.as_ref() {
// Probably a link of the form `<http://example.com>`
Expand All @@ -560,17 +541,16 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
headers.safety |= in_heading && text.trim() == "Safety";
headers.errors |= in_heading && text.trim() == "Errors";
headers.panics |= in_heading && text.trim() == "Panics";
if in_code {
if in_code_block {
if is_rust {
let edition = edition.unwrap_or_else(|| cx.tcx.sess.edition());
check_code(cx, &text, edition, span);
}
} else {
// Adjust for the beginning of the current `Event`
let span = span.with_lo(span.lo() + BytePos::from_usize(range.start - begin));
check_text(cx, valid_idents, &text, span, &prev_was_ticks, &mut ticks_inverted);
check_text(cx, valid_idents, &text, span, &prev_was_code, &mut ticks_inverted);
}
prev_was_ticks = None;
}
},
}
Expand Down Expand Up @@ -654,11 +634,11 @@ fn check_text(
valid_idents: &FxHashSet<String>,
text: &str,
span: Span,
prev_was_ticks: &Option<Span>,
prev_was_code: &Option<Span>,
ticks_inverted: &mut bool,
) {
if_chain! {
if let Some(prev_span) = prev_was_ticks;
if let Some(prev_span) = prev_was_code;
let words = text.split(char::is_whitespace).collect_vec();
if words.len() == 1;
if should_have_backticks(text);
Expand Down

0 comments on commit 0fdcaa8

Please sign in to comment.