Skip to content

Commit 97e73ee

Browse files
committed
doc links: Filter away autolinks in both rustc and rustdoc
1 parent ccdb598 commit 97e73ee

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

compiler/rustc_resolve/src/rustdoc.rs

+19-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use pulldown_cmark::{BrokenLink, Event, Options, Parser, Tag};
1+
use pulldown_cmark::{BrokenLink, Event, LinkType, Options, Parser, Tag};
22
use rustc_ast as ast;
33
use rustc_ast::util::comments::beautify_doc_string;
44
use rustc_data_structures::fx::FxHashMap;
@@ -347,6 +347,21 @@ fn preprocess_link(link: &str) -> String {
347347
strip_generics_from_path(link).unwrap_or_else(|_| link.to_string())
348348
}
349349

350+
/// Keep inline and reference links `[]`,
351+
/// but skip autolinks `<>` which we never consider to be intra-doc links.
352+
pub fn may_be_doc_link(link_type: LinkType) -> bool {
353+
match link_type {
354+
LinkType::Inline
355+
| LinkType::Reference
356+
| LinkType::ReferenceUnknown
357+
| LinkType::Collapsed
358+
| LinkType::CollapsedUnknown
359+
| LinkType::Shortcut
360+
| LinkType::ShortcutUnknown => true,
361+
LinkType::Autolink | LinkType::Email => false,
362+
}
363+
}
364+
350365
/// Simplified version of `preprocessed_markdown_links` from rustdoc.
351366
/// Must return at least the same links as it, but may add some more links on top of that.
352367
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
359374
Some(&mut |link: BrokenLink<'_>| Some((link.reference, "".into()))),
360375
)
361376
.filter_map(|event| match event {
362-
Event::Start(Tag::Link(_, dest, _)) => Some(preprocess_link(&dest)),
377+
Event::Start(Tag::Link(link_type, dest, _)) if may_be_doc_link(link_type) => {
378+
Some(preprocess_link(&dest))
379+
}
363380
_ => None,
364381
})
365382
.collect()

src/librustdoc/html/markdown.rs

+8-16
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use rustc_data_structures::fx::FxHashMap;
2929
use rustc_hir::def_id::DefId;
3030
use rustc_middle::ty::TyCtxt;
3131
pub(crate) use rustc_resolve::rustdoc::main_body_opts;
32+
use rustc_resolve::rustdoc::may_be_doc_link;
3233
use rustc_span::edition::Edition;
3334
use rustc_span::{Span, Symbol};
3435

@@ -1269,22 +1270,13 @@ pub(crate) fn markdown_links<R>(
12691270
)
12701271
.into_offset_iter()
12711272
.filter_map(|(event, span)| match event {
1272-
Event::Start(Tag::Link(
1273-
// `<>` links cannot be intra-doc links so we skip them.
1274-
kind @ (LinkType::Inline
1275-
| LinkType::Reference
1276-
| LinkType::ReferenceUnknown
1277-
| LinkType::Collapsed
1278-
| LinkType::CollapsedUnknown
1279-
| LinkType::Shortcut
1280-
| LinkType::ShortcutUnknown),
1281-
dest,
1282-
_,
1283-
)) => preprocess_link(MarkdownLink {
1284-
kind,
1285-
range: span_for_link(&dest, span),
1286-
link: dest.into_string(),
1287-
}),
1273+
Event::Start(Tag::Link(link_type, dest, _)) if may_be_doc_link(link_type) => {
1274+
preprocess_link(MarkdownLink {
1275+
kind: link_type,
1276+
range: span_for_link(&dest, span),
1277+
link: dest.into_string(),
1278+
})
1279+
}
12881280
_ => None,
12891281
})
12901282
.collect()

0 commit comments

Comments
 (0)