Skip to content

Commit d34a088

Browse files
committed
check for unbalanced tick pairs in doc-markdown
1 parent 37bbc6b commit d34a088

File tree

4 files changed

+58
-20
lines changed

4 files changed

+58
-20
lines changed

clippy_lints/src/doc.rs

+40-18
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,8 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
485485
let mut in_heading = false;
486486
let mut is_rust = false;
487487
let mut edition = None;
488+
let mut ticks_unbalanced = false;
489+
let mut text_to_check = Vec::new();
488490
for (event, range) in events {
489491
match event {
490492
Start(CodeBlock(ref kind)) => {
@@ -516,34 +518,54 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
516518
Html(_html) => (), // HTML is weird, just ignore it
517519
SoftBreak | HardBreak | TaskListMarker(_) | Code(_) | Rule => (),
518520
FootnoteReference(text) | Text(text) => {
519-
if Some(&text) == in_link.as_ref() {
521+
if Some(&text) == in_link.as_ref() || ticks_unbalanced {
520522
// Probably a link of the form `<http://example.com>`
521523
// Which are represented as a link to "http://example.com" with
522524
// text "http://example.com" by pulldown-cmark
523525
continue;
524526
}
525-
headers.safety |= in_heading && text.trim() == "Safety";
526-
headers.errors |= in_heading && text.trim() == "Errors";
527-
headers.panics |= in_heading && text.trim() == "Panics";
528-
let index = match spans.binary_search_by(|c| c.0.cmp(&range.start)) {
529-
Ok(o) => o,
530-
Err(e) => e - 1,
531-
};
532-
let (begin, span) = spans[index];
533-
if in_code {
534-
if is_rust {
535-
let edition = edition.unwrap_or_else(|| cx.tcx.sess.edition());
536-
check_code(cx, &text, edition, span);
537-
}
527+
if text.contains('`') && !in_code {
528+
let (_, start) = spans[0];
529+
let span = if let Some((_, end)) = spans.last() {
530+
start.with_hi(end.hi())
531+
} else {
532+
start
533+
};
534+
span_lint(
535+
cx,
536+
DOC_MARKDOWN,
537+
span,
538+
"backticks are unbalanced; one may be missing a pair",
539+
);
540+
ticks_unbalanced = true;
538541
} else {
539-
// Adjust for the beginning of the current `Event`
540-
let span = span.with_lo(span.lo() + BytePos::from_usize(range.start - begin));
541-
542-
check_text(cx, valid_idents, &text, span);
542+
headers.safety |= in_heading && text.trim() == "Safety";
543+
headers.errors |= in_heading && text.trim() == "Errors";
544+
headers.panics |= in_heading && text.trim() == "Panics";
545+
let index = match spans.binary_search_by(|c| c.0.cmp(&range.start)) {
546+
Ok(o) => o,
547+
Err(e) => e - 1,
548+
};
549+
let (begin, span) = spans[index];
550+
if in_code {
551+
if is_rust {
552+
let edition = edition.unwrap_or_else(|| cx.tcx.sess.edition());
553+
check_code(cx, &text, edition, span);
554+
}
555+
} else {
556+
// Adjust for the beginning of the current `Event`
557+
let span = span.with_lo(span.lo() + BytePos::from_usize(range.start - begin));
558+
text_to_check.push((text, span));
559+
}
543560
}
544561
},
545562
}
546563
}
564+
if !ticks_unbalanced {
565+
for (text, span) in text_to_check {
566+
check_text(cx, valid_idents, &text, span);
567+
}
568+
}
547569
headers
548570
}
549571

clippy_lints/src/if_let_mutex.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ impl<'tcx> LateLintPass<'tcx> for IfLetMutex {
8383
}
8484
}
8585

86-
/// Checks if `Mutex::lock` is called in the `if let _ = expr.
86+
/// Checks if `Mutex::lock` is called in the `if let` expr.
8787
pub struct OppVisitor<'a, 'tcx> {
8888
mutex_lock_called: bool,
8989
found_mutex: Option<&'tcx Expr<'tcx>>,

tests/ui/doc.rs

+6
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,12 @@ fn issue_2343() {}
203203
/// __|_ _|__||_|
204204
fn pulldown_cmark_crash() {}
205205

206+
/// This is a doc comment with `unbalanced_tick marks and several words that
207+
/// should be `encompassed_by` tick marks because they `contain_underscores`.
208+
/// Because of the initial `unbalanced_tick` pair, the error message is
209+
/// very `confusing_and_misleading`.
210+
fn issue_6753() {}
211+
206212
// issue #7033 - const_evaluatable_checked ICE
207213
struct S<T, const N: usize>
208214
where [(); N.checked_next_power_of_two().unwrap()]: {

tests/ui/doc.stderr

+11-1
Original file line numberDiff line numberDiff line change
@@ -186,5 +186,15 @@ error: you should put `mycrate::Collection` between ticks in the documentation
186186
LL | /// An iterator over mycrate::Collection's values.
187187
| ^^^^^^^^^^^^^^^^^^^
188188

189-
error: aborting due to 31 previous errors
189+
error: backticks are unbalanced; one may be missing a pair
190+
--> $DIR/doc.rs:206:4
191+
|
192+
LL | /// This is a doc comment with `unbalanced_tick marks and several words that
193+
| ____^
194+
LL | | /// should be `encompassed_by` tick marks because they `contain_underscores`.
195+
LL | | /// Because of the initial `unbalanced_tick` pair, the error message is
196+
LL | | /// very `confusing_and_misleading`.
197+
| |____________________________________^
198+
199+
error: aborting due to 32 previous errors
190200

0 commit comments

Comments
 (0)