Skip to content

Commit ca4b042

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 ae9173d commit ca4b042

File tree

3 files changed

+61
-4
lines changed

3 files changed

+61
-4
lines changed

src/librustdoc/passes/collect_intra_doc_links.rs

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

939944
// Bail early for real links.
940-
if ori_link.link.contains('/') {
945+
if can_be_url && ori_link.link.contains('/') {
941946
return None;
942947
}
943948

@@ -962,7 +967,7 @@ fn preprocess_link(
962967
Ok(None) => (None, link, link),
963968
Err((err_msg, relative_range)) => {
964969
// Only report error if we would not have ignored this link. See issue #83859.
965-
if !should_ignore_link_with_disambiguators(link) {
970+
if can_be_url && !should_ignore_link_with_disambiguators(link) {
966971
let disambiguator_range = match range_between_backticks(&ori_link.range, dox) {
967972
MarkdownLinkRange::Destination(no_backticks_range) => {
968973
MarkdownLinkRange::Destination(
@@ -979,7 +984,7 @@ fn preprocess_link(
979984
}
980985
};
981986

982-
if should_ignore_link(path_str) {
987+
if can_be_url && should_ignore_link(path_str) {
983988
return None;
984989
}
985990

@@ -996,7 +1001,7 @@ fn preprocess_link(
9961001
assert!(!path_str.contains(['<', '>'].as_slice()));
9971002

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

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`]
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

Lines changed: 39 additions & 0 deletions
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)