Skip to content

Commit 6a7d488

Browse files
lolbinarycatfmease
andcommitted
rustdoc::broken_intra_doc_links: only be lenient with shortcut links
collapsed links and reference links have a pretty particular syntax, it seems unlikely they would show up on accident. Co-authored-by: León Orell Valerian Liehr <[email protected]>
1 parent 0413481 commit 6a7d488

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

src/librustdoc/passes/collect_intra_doc_links.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -944,9 +944,10 @@ fn preprocess_link(
944944
// certain link kinds cannot have their path be urls,
945945
// so they should not be ignored, no matter how much they look like urls.
946946
// 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;
947+
let can_be_url = !matches!(
948+
ori_link.kind,
949+
LinkType::ShortcutUnknown | LinkType::CollapsedUnknown | LinkType::ReferenceUnknown
950+
);
950951

951952
// [] is mostly likely not supposed to be a link
952953
if ori_link.link.is_empty() {
@@ -996,9 +997,25 @@ fn preprocess_link(
996997
}
997998
};
998999

999-
// If there's no backticks, be lenient revert to old behavior.
1000+
// If there's no backticks, be lenient and revert to the old behavior.
10001001
// This is to prevent churn by linting on stuff that isn't meant to be a link.
1001-
if (can_be_url || !ori_link.link.contains('`')) && should_ignore_link(path_str) {
1002+
// only shortcut links have simple enough syntax that they
1003+
// are likely to be written accidentally, collapsed and reference links
1004+
// need 4 metachars, and reference links will not usually use
1005+
// backticks in the reference name.
1006+
// therefore, only shortcut syntax gets the lenient behavior.
1007+
//
1008+
// here's a truth table for how link kinds that cannot be urls are handled:
1009+
//
1010+
// |-------------------------------------------------------|
1011+
// | | is shortcut link | not shortcut link |
1012+
// |--------------|--------------------|-------------------|
1013+
// | has backtick | never ignore | never ignore |
1014+
// | no backtick | ignore if url-like | never ignore |
1015+
// |-------------------------------------------------------|
1016+
let ignore_urllike =
1017+
can_be_url || (ori_link.kind == LinkType::ShortcutUnknown && !ori_link.link.contains('`'));
1018+
if ignore_urllike && should_ignore_link(path_str) {
10021019
return None;
10031020
}
10041021

tests/rustdoc-ui/intra-doc/weird-syntax.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pub struct XLinkToCloneWithStartSpace;
6565
/// [x][struct@Clone ] //~ERROR link
6666
pub struct XLinkToCloneWithEndSpace;
6767

68-
/// [x][Clone\(\)]
68+
/// [x][Clone\(\)] //~ERROR link
6969
pub struct XLinkToCloneWithEscapedParens;
7070

7171
/// [x][`Clone`] not URL-shaped enough

tests/rustdoc-ui/intra-doc/weird-syntax.stderr

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,14 @@ LL - /// [x][struct@Clone ]
123123
LL + /// [x][trait@Clone ]
124124
|
125125

126+
error: unresolved link to `Clone\(\)`
127+
--> $DIR/weird-syntax.rs:68:9
128+
|
129+
LL | /// [x][Clone\(\)]
130+
| ^^^^^^^^^ no item named `Clone\(\)` in scope
131+
|
132+
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
133+
126134
error: unresolved link to `Clone`
127135
--> $DIR/weird-syntax.rs:74:9
128136
|
@@ -299,5 +307,5 @@ LL | /// - [`SDL_PROP_WINDOW_CREATE_COCOA_WINDOW_POINTER`]: the
299307
|
300308
= help: to escape `[` and `]` characters, add '\' before them like `\[` or `\]`
301309

302-
error: aborting due to 27 previous errors
310+
error: aborting due to 28 previous errors
303311

0 commit comments

Comments
 (0)