about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2021-10-06 19:16:22 +0000
committerbors <bors@rust-lang.org>2021-10-06 19:16:22 +0000
commit11492c75a39b26c19f3e5655d425b2db8a0b4e00 (patch)
treec9d0463f86ad7e47d46f474bbcc1a756a533db33
parentb9d753e4f5eb921a4185d5bbaef15a5af2c43e2f (diff)
parent2a8d7bd0dd69ab1fe2a2af0f2984959f62b5631c (diff)
downloadrust-11492c75a39b26c19f3e5655d425b2db8a0b4e00.tar.gz
rust-11492c75a39b26c19f3e5655d425b2db8a0b4e00.zip
Auto merge of #7772 - Manishearth:doc-markdown-intra, r=camsteffen
Handle intra-doc links in doc_markdown

Fixes #7758

changelog: Handle intra-doc links in [`doc_markdown`]
-rw-r--r--clippy_lints/src/doc.rs14
-rw-r--r--tests/ui/doc/doc.rs5
2 files changed, 18 insertions, 1 deletions
diff --git a/clippy_lints/src/doc.rs b/clippy_lints/src/doc.rs
index 55d2265125e..75873fd7c1c 100644
--- a/clippy_lints/src/doc.rs
+++ b/clippy_lints/src/doc.rs
@@ -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::unnecessary_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![];
 
@@ -440,7 +449,10 @@ 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;
diff --git a/tests/ui/doc/doc.rs b/tests/ui/doc/doc.rs
index 8b0c0f304fc..342208e52b8 100644
--- a/tests/ui/doc/doc.rs
+++ b/tests/ui/doc/doc.rs
@@ -203,6 +203,11 @@ fn issue_2343() {}
 /// __|_ _|__||_|
 fn pulldown_cmark_crash() {}
 
+/// This should not lint
+/// (regression test for #7758)
+/// [plain text][path::to::item]
+fn intra_doc_link() {}
+
 // issue #7033 - generic_const_exprs ICE
 struct S<T, const N: usize>
 where [(); N.checked_next_power_of_two().unwrap()]: {