Skip to content

Commit b3fda87

Browse files
committed
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.
1 parent 5485535 commit b3fda87

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

src/librustdoc/passes/collect_intra_doc_links.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -988,7 +988,23 @@ fn preprocess_link(
988988

989989
// If there's no backticks, be lenient revert to old behavior.
990990
// This is to prevent churn by linting on stuff that isn't meant to be a link.
991-
if (can_be_url || !ori_link.link.contains('`')) && should_ignore_link(path_str) {
991+
// only shortcut links have simple enough syntax that they
992+
// are likely to be written accidentlly, collapsed and reference links
993+
// need 4 metachars, and reference links will not usually use
994+
// backticks in the reference name.
995+
// therefore, only shortcut syntax gets the lenient behavior.
996+
//
997+
// here's a truth table for how link kinds that cannot be urls are handled:
998+
//
999+
// |-------------------------------------------------------|
1000+
// | | is shortcut link | not shortcut link |
1001+
// |--------------|--------------------|-------------------|
1002+
// | has backtick | never ignore | never ignore |
1003+
// | no backtick | ignore if url-like | never ignore |
1004+
// |-------------------------------------------------------|
1005+
let ignore_urllike =
1006+
can_be_url || (ori_link.kind == LinkType::ShortcutUnknown && !ori_link.link.contains('`'));
1007+
if ignore_urllike && should_ignore_link(path_str) {
9921008
return None;
9931009
}
9941010

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)