Skip to content

Commit 2a8d7bd

Browse files
committed
Handle intra-doc links in doc_markdown
1 parent 80408ef commit 2a8d7bd

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

clippy_lints/src/doc.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,15 @@ struct DocHeaders {
406406
}
407407

408408
fn check_attrs<'a>(cx: &LateContext<'_>, valid_idents: &FxHashSet<String>, attrs: &'a [Attribute]) -> DocHeaders {
409+
use pulldown_cmark::{BrokenLink, CowStr, Options};
410+
/// We don't want the parser to choke on intra doc links. Since we don't
411+
/// actually care about rendering them, just pretend that all broken links are
412+
/// point to a fake address.
413+
#[allow(clippy::unnecessary_wraps)] // we're following a type signature
414+
fn fake_broken_link_callback<'a>(_: BrokenLink<'_>) -> Option<(CowStr<'a>, CowStr<'a>)> {
415+
Some(("fake".into(), "fake".into()))
416+
}
417+
409418
let mut doc = String::new();
410419
let mut spans = vec![];
411420

@@ -440,7 +449,10 @@ fn check_attrs<'a>(cx: &LateContext<'_>, valid_idents: &FxHashSet<String>, attrs
440449
};
441450
}
442451

443-
let parser = pulldown_cmark::Parser::new(&doc).into_offset_iter();
452+
let mut cb = fake_broken_link_callback;
453+
454+
let parser =
455+
pulldown_cmark::Parser::new_with_broken_link_callback(&doc, Options::empty(), Some(&mut cb)).into_offset_iter();
444456
// Iterate over all `Events` and combine consecutive events into one
445457
let events = parser.coalesce(|previous, current| {
446458
use pulldown_cmark::Event::Text;

tests/ui/doc/doc.stderr

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

189-
error: you should put `text][path::to::item` between ticks in the documentation
190-
--> $DIR/doc.rs:208:12
191-
|
192-
LL | /// [plain text][path::to::item]
193-
| ^^^^^^^^^^^^^^^^^^^^
194-
195-
error: aborting due to 32 previous errors
189+
error: aborting due to 31 previous errors
196190

0 commit comments

Comments
 (0)