Skip to content

Commit 5ae2d42

Browse files
get rid of some false negatives in rustdoc::broken_intra_doc_links
rustdoc will not try to do intra-doc linking if the "path" of a link looks too much like a "real url". however, only inline links ([text](url)) can actually contain a url, other types of links (reference links, shortcut links) contain a *reference* which is later resolved to an actual url. the "path" in this case cannot be a url, and therefore it should not be skipped due to looking like a url. Co-authored-by: Michael Howell <[email protected]>
1 parent fc5af18 commit 5ae2d42

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

src/librustdoc/passes/collect_intra_doc_links.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -941,13 +941,20 @@ fn preprocess_link(
941941
ori_link: &MarkdownLink,
942942
dox: &str,
943943
) -> Option<Result<PreprocessingInfo, PreprocessingError>> {
944+
// certain link kinds cannot have their path be urls,
945+
// so they should not be ignored, no matter how much they look like urls.
946+
// e.g. [https://example.com/] is not a link to example.com.
947+
let can_be_url = ori_link.kind != LinkType::ShortcutUnknown
948+
&& ori_link.kind != LinkType::CollapsedUnknown
949+
&& ori_link.kind != LinkType::ReferenceUnknown;
950+
944951
// [] is mostly likely not supposed to be a link
945952
if ori_link.link.is_empty() {
946953
return None;
947954
}
948955

949956
// Bail early for real links.
950-
if ori_link.link.contains('/') {
957+
if can_be_url && ori_link.link.contains('/') {
951958
return None;
952959
}
953960

@@ -972,7 +979,7 @@ fn preprocess_link(
972979
Ok(None) => (None, link, link),
973980
Err((err_msg, relative_range)) => {
974981
// Only report error if we would not have ignored this link. See issue #83859.
975-
if !should_ignore_link_with_disambiguators(link) {
982+
if !(can_be_url && should_ignore_link_with_disambiguators(link)) {
976983
let disambiguator_range = match range_between_backticks(&ori_link.range, dox) {
977984
MarkdownLinkRange::Destination(no_backticks_range) => {
978985
MarkdownLinkRange::Destination(
@@ -989,7 +996,7 @@ fn preprocess_link(
989996
}
990997
};
991998

992-
if should_ignore_link(path_str) {
999+
if can_be_url && should_ignore_link(path_str) {
9931000
return None;
9941001
}
9951002

tests/rustdoc-ui/bad-intra-doc.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#![no_std]
2+
#![deny(rustdoc::broken_intra_doc_links)]
3+
4+
// regression test for https://github.com/rust-lang/rust/issues/54191
5+
6+
/// this is not a link to [`example.com`] //~ERROR unresolved link
7+
///
8+
/// this link [`has spaces in it`].
9+
///
10+
/// attempted link to method: [`Foo.bar()`] //~ERROR unresolved link
11+
///
12+
/// classic broken intra-doc link: [`Bar`] //~ERROR unresolved link
13+
pub struct Foo;

tests/rustdoc-ui/bad-intra-doc.stderr

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
error: unresolved link to `example.com`
2+
--> $DIR/bad-intra-doc.rs:6:29
3+
|
4+
LL | /// this is not a link to [`example.com`]
5+
| ^^^^^^^^^^^ no item named `example.com` in scope
6+
|
7+
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
8+
note: the lint level is defined here
9+
--> $DIR/bad-intra-doc.rs:2:9
10+
|
11+
LL | #![deny(rustdoc::broken_intra_doc_links)]
12+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
13+
14+
error: unresolved link to `Foo.bar`
15+
--> $DIR/bad-intra-doc.rs:10:33
16+
|
17+
LL | /// attempted link to method: [`Foo.bar()`]
18+
| ^^^^^^^^^ no item named `Foo.bar` in scope
19+
|
20+
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
21+
22+
error: unresolved link to `Bar`
23+
--> $DIR/bad-intra-doc.rs:12:38
24+
|
25+
LL | /// classic broken intra-doc link: [`Bar`]
26+
| ^^^ no item named `Bar` in scope
27+
|
28+
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
29+
30+
error: aborting due to 3 previous errors
31+

0 commit comments

Comments
 (0)