about summary refs log tree commit diff
path: root/compiler/rustc_resolve/src/rustdoc.rs
diff options
context:
space:
mode:
authorVadim Petrochenkov <vadim.petrochenkov@gmail.com>2023-02-18 14:56:31 +0400
committerVadim Petrochenkov <vadim.petrochenkov@gmail.com>2023-02-18 15:15:57 +0400
commit97e73eea84e8302ae08f88645538ee27485770d6 (patch)
treee33cd6aef1cfaded4dbdb1ecade0d0627cab5d44 /compiler/rustc_resolve/src/rustdoc.rs
parentccdb598d1b495952bdb09122dc53c39b40f23758 (diff)
downloadrust-97e73eea84e8302ae08f88645538ee27485770d6.tar.gz
rust-97e73eea84e8302ae08f88645538ee27485770d6.zip
doc links: Filter away autolinks in both rustc and rustdoc
Diffstat (limited to 'compiler/rustc_resolve/src/rustdoc.rs')
-rw-r--r--compiler/rustc_resolve/src/rustdoc.rs21
1 files changed, 19 insertions, 2 deletions
diff --git a/compiler/rustc_resolve/src/rustdoc.rs b/compiler/rustc_resolve/src/rustdoc.rs
index be49fbfaa29..0c4b4bc3f62 100644
--- a/compiler/rustc_resolve/src/rustdoc.rs
+++ b/compiler/rustc_resolve/src/rustdoc.rs
@@ -1,4 +1,4 @@
-use pulldown_cmark::{BrokenLink, Event, Options, Parser, Tag};
+use pulldown_cmark::{BrokenLink, Event, LinkType, Options, Parser, Tag};
 use rustc_ast as ast;
 use rustc_ast::util::comments::beautify_doc_string;
 use rustc_data_structures::fx::FxHashMap;
@@ -347,6 +347,21 @@ fn preprocess_link(link: &str) -> String {
     strip_generics_from_path(link).unwrap_or_else(|_| link.to_string())
 }
 
+/// Keep inline and reference links `[]`,
+/// but skip autolinks `<>` which we never consider to be intra-doc links.
+pub fn may_be_doc_link(link_type: LinkType) -> bool {
+    match link_type {
+        LinkType::Inline
+        | LinkType::Reference
+        | LinkType::ReferenceUnknown
+        | LinkType::Collapsed
+        | LinkType::CollapsedUnknown
+        | LinkType::Shortcut
+        | LinkType::ShortcutUnknown => true,
+        LinkType::Autolink | LinkType::Email => false,
+    }
+}
+
 /// Simplified version of `preprocessed_markdown_links` from rustdoc.
 /// Must return at least the same links as it, but may add some more links on top of that.
 pub(crate) fn attrs_to_preprocessed_links(attrs: &[ast::Attribute]) -> Vec<String> {
@@ -359,7 +374,9 @@ pub(crate) fn attrs_to_preprocessed_links(attrs: &[ast::Attribute]) -> Vec<Strin
         Some(&mut |link: BrokenLink<'_>| Some((link.reference, "".into()))),
     )
     .filter_map(|event| match event {
-        Event::Start(Tag::Link(_, dest, _)) => Some(preprocess_link(&dest)),
+        Event::Start(Tag::Link(link_type, dest, _)) if may_be_doc_link(link_type) => {
+            Some(preprocess_link(&dest))
+        }
         _ => None,
     })
     .collect()