Skip to content

Commit 8ae6586

Browse files
committed
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. fixes rust-lang#54191
1 parent 7028d93 commit 8ae6586

File tree

3 files changed

+61
-4
lines changed

3 files changed

+61
-4
lines changed

src/librustdoc/passes/collect_intra_doc_links.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -930,13 +930,18 @@ fn preprocess_link(
930930
ori_link: &MarkdownLink,
931931
dox: &str,
932932
) -> Option<Result<PreprocessingInfo, PreprocessingError>> {
933+
// certain link kinds cannot have their path be urls,
934+
// so they should not be ignored, no matter how much they look like urls.
935+
// e.g. [https://example.com/] is not a link to example.com.
936+
let can_be_url = ori_link.kind == LinkType::Inline || ori_link.kind == LinkType::Autolink;
937+
933938
// [] is mostly likely not supposed to be a link
934939
if ori_link.link.is_empty() {
935940
return None;
936941
}
937942

938943
// Bail early for real links.
939-
if ori_link.link.contains('/') {
944+
if can_be_url && ori_link.link.contains('/') {
940945
return None;
941946
}
942947

@@ -961,7 +966,7 @@ fn preprocess_link(
961966
Ok(None) => (None, link, link),
962967
Err((err_msg, relative_range)) => {
963968
// Only report error if we would not have ignored this link. See issue #83859.
964-
if !should_ignore_link_with_disambiguators(link) {
969+
if can_be_url && !should_ignore_link_with_disambiguators(link) {
965970
let disambiguator_range = match range_between_backticks(&ori_link.range, dox) {
966971
MarkdownLinkRange::Destination(no_backticks_range) => {
967972
MarkdownLinkRange::Destination(
@@ -978,7 +983,7 @@ fn preprocess_link(
978983
}
979984
};
980985

981-
if should_ignore_link(path_str) {
986+
if can_be_url && should_ignore_link(path_str) {
982987
return None;
983988
}
984989

@@ -995,7 +1000,7 @@ fn preprocess_link(
9951000
assert!(!path_str.contains(['<', '>'].as_slice()));
9961001

9971002
// The link is not an intra-doc link if it still contains spaces after stripping generics.
998-
if path_str.contains(' ') {
1003+
if can_be_url && path_str.contains(' ') {
9991004
return None;
10001005
}
10011006

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

+13
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`]
7+
///
8+
/// this link [`has spaces in it`].
9+
///
10+
/// attempted link to method: [`Foo.bar()`]
11+
///
12+
/// classic broken intra-doc link: [`Bar`]
13+
pub struct Foo;

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

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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 `has spaces in it`
15+
--> $DIR/bad-intra-doc.rs:8:17
16+
|
17+
LL | /// this link [`has spaces in it`].
18+
| ^^^^^^^^^^^^^^^^ no item named `has spaces in it` in scope
19+
|
20+
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
21+
22+
error: unresolved link to `Foo.bar`
23+
--> $DIR/bad-intra-doc.rs:10:33
24+
|
25+
LL | /// attempted link to method: [`Foo.bar()`]
26+
| ^^^^^^^^^ no item named `Foo.bar` in scope
27+
|
28+
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
29+
30+
error: unresolved link to `Bar`
31+
--> $DIR/bad-intra-doc.rs:12:38
32+
|
33+
LL | /// classic broken intra-doc link: [`Bar`]
34+
| ^^^ no item named `Bar` in scope
35+
|
36+
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
37+
38+
error: aborting due to 4 previous errors
39+

0 commit comments

Comments
 (0)