Skip to content

Commit

Permalink
Handle intra-doc links in doc_markdown
Browse files Browse the repository at this point in the history
  • Loading branch information
Manishearth committed Oct 6, 2021
1 parent 007921f commit c135499
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
15 changes: 14 additions & 1 deletion clippy_lints/src/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,15 @@ struct DocHeaders {
}

fn check_attrs<'a>(cx: &LateContext<'_>, valid_idents: &FxHashSet<String>, attrs: &'a [Attribute]) -> DocHeaders {
use pulldown_cmark::{BrokenLink, CowStr, Options};
/// We don't want the parser to choke on intra doc links. Since we don't
/// actually care about rendering them, just pretend that all broken links are
/// point to a fake address.
#[allow(clippy::unecessary_wraps)] // we're following a type signature
fn fake_broken_link_callback<'a>(_: BrokenLink<'_>) -> Option<(CowStr<'a>, CowStr<'a>)> {
Some(("fake".into(), "fake".into()))
}

let mut doc = String::new();
let mut spans = vec![];

Expand Down Expand Up @@ -440,7 +449,11 @@ fn check_attrs<'a>(cx: &LateContext<'_>, valid_idents: &FxHashSet<String>, attrs
};
}

let parser = pulldown_cmark::Parser::new(&doc).into_offset_iter();

let mut cb = fake_broken_link_callback;

let parser =
pulldown_cmark::Parser::new_with_broken_link_callback(&doc, Options::empty(), Some(&mut cb)).into_offset_iter();
// Iterate over all `Events` and combine consecutive events into one
let events = parser.coalesce(|previous, current| {
use pulldown_cmark::Event::Text;
Expand Down
8 changes: 1 addition & 7 deletions tests/ui/doc/doc.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,5 @@ error: you should put `mycrate::Collection` between ticks in the documentation
LL | /// An iterator over mycrate::Collection's values.
| ^^^^^^^^^^^^^^^^^^^

error: you should put `text][path::to::item` between ticks in the documentation
--> $DIR/doc.rs:208:12
|
LL | /// [plain text][path::to::item]
| ^^^^^^^^^^^^^^^^^^^^

error: aborting due to 32 previous errors
error: aborting due to 31 previous errors

0 comments on commit c135499

Please sign in to comment.