Skip to content

Commit 140392b

Browse files
committed
Adjust rustdoc automatic link suggestion
Use more accurate spans for multipart suggestion.
1 parent 1cfd47f commit 140392b

File tree

5 files changed

+117
-39
lines changed

5 files changed

+117
-39
lines changed

src/librustdoc/passes/lint/bare_urls.rs

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,22 @@ pub(super) fn visit_item(cx: &DocContext<'_>, item: &Item) {
1919
};
2020
let dox = item.doc_value();
2121
if !dox.is_empty() {
22-
let report_diag =
23-
|cx: &DocContext<'_>, msg: &'static str, url: &str, range: Range<usize>| {
24-
let sp =
25-
source_span_for_markdown_range(cx.tcx, &dox, &range, &item.attrs.doc_strings)
26-
.unwrap_or_else(|| item.attr_span(cx.tcx));
27-
cx.tcx.node_span_lint(crate::lint::BARE_URLS, hir_id, sp, |lint| {
28-
lint.primary_message(msg)
29-
.note("bare URLs are not automatically turned into clickable links")
30-
.span_suggestion(
31-
sp,
32-
"use an automatic link instead",
33-
format!("<{url}>"),
34-
Applicability::MachineApplicable,
35-
);
36-
});
37-
};
22+
let report_diag = |cx: &DocContext<'_>, msg: &'static str, range: Range<usize>| {
23+
let sp = source_span_for_markdown_range(cx.tcx, &dox, &range, &item.attrs.doc_strings)
24+
.unwrap_or_else(|| item.attr_span(cx.tcx));
25+
cx.tcx.node_span_lint(crate::lint::BARE_URLS, hir_id, sp, |lint| {
26+
lint.primary_message(msg)
27+
.note("bare URLs are not automatically turned into clickable links")
28+
.multipart_suggestion(
29+
"use an automatic link instead",
30+
vec![
31+
(sp.shrink_to_lo(), "<".to_string()),
32+
(sp.shrink_to_hi(), ">".to_string()),
33+
],
34+
Applicability::MachineApplicable,
35+
);
36+
});
37+
};
3838

3939
let mut p = Parser::new_ext(&dox, main_body_opts()).into_offset_iter();
4040

@@ -74,17 +74,15 @@ fn find_raw_urls(
7474
cx: &DocContext<'_>,
7575
text: &str,
7676
range: Range<usize>,
77-
f: &impl Fn(&DocContext<'_>, &'static str, &str, Range<usize>),
77+
f: &impl Fn(&DocContext<'_>, &'static str, Range<usize>),
7878
) {
7979
trace!("looking for raw urls in {text}");
8080
// For now, we only check "full" URLs (meaning, starting with "http://" or "https://").
8181
for match_ in URL_REGEX.find_iter(text) {
82-
let url = match_.as_str();
8382
let url_range = match_.range();
8483
f(
8584
cx,
8685
"this URL is not a hyperlink",
87-
url,
8886
Range { start: range.start + url_range.start, end: range.start + url_range.end },
8987
);
9088
}

tests/rustdoc-ui/diagnostic-width.stderr

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@ error: this URL is not a hyperlink
22
--> $DIR/diagnostic-width.rs:4:41
33
|
44
LL | ... a http://link.com
5-
| ^^^^^^^^^^^^^^^ help: use an automatic link instead: `<http://link.com>`
5+
| ^^^^^^^^^^^^^^^
66
|
77
= note: bare URLs are not automatically turned into clickable links
88
note: the lint level is defined here
99
--> $DIR/diagnostic-width.rs:2:9
1010
|
1111
LL | ...ny(rustdoc::bare_url...
1212
| ^^^^^^^^^^^^^^^^^^
13+
help: use an automatic link instead
14+
|
15+
LL | /// This is a long line that contains a <http://link.com>
16+
| + +
1317

1418
error: aborting due to 1 previous error
1519

tests/rustdoc-ui/include-str-bare-urls.stderr

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@ error: this URL is not a hyperlink
22
--> $DIR/auxiliary/include-str-bare-urls.md:1:11
33
|
44
LL | HEADS UP! https://example.com MUST SHOW UP IN THE STDERR FILE!
5-
| ^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://example.com>`
5+
| ^^^^^^^^^^^^^^^^^^^
66
|
77
= note: bare URLs are not automatically turned into clickable links
88
note: the lint level is defined here
99
--> $DIR/include-str-bare-urls.rs:14:9
1010
|
1111
LL | #![deny(rustdoc::bare_urls)]
1212
| ^^^^^^^^^^^^^^^^^^
13+
help: use an automatic link instead
14+
|
15+
LL | HEADS UP! <https://example.com> MUST SHOW UP IN THE STDERR FILE!
16+
| + +
1317

1418
error: aborting due to 1 previous error
1519

tests/rustdoc-ui/lints/bare-urls.stderr

Lines changed: 85 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,142 +2,210 @@ error: this URL is not a hyperlink
22
--> $DIR/bare-urls.rs:5:5
33
|
44
LL | /// https://somewhere.com
5-
| ^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://somewhere.com>`
5+
| ^^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: bare URLs are not automatically turned into clickable links
88
note: the lint level is defined here
99
--> $DIR/bare-urls.rs:3:9
1010
|
1111
LL | #![deny(rustdoc::bare_urls)]
1212
| ^^^^^^^^^^^^^^^^^^
13+
help: use an automatic link instead
14+
|
15+
LL | /// <https://somewhere.com>
16+
| + +
1317

1418
error: this URL is not a hyperlink
1519
--> $DIR/bare-urls.rs:7:5
1620
|
1721
LL | /// https://somewhere.com/a
18-
| ^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://somewhere.com/a>`
22+
| ^^^^^^^^^^^^^^^^^^^^^^^
1923
|
2024
= note: bare URLs are not automatically turned into clickable links
25+
help: use an automatic link instead
26+
|
27+
LL | /// <https://somewhere.com/a>
28+
| + +
2129

2230
error: this URL is not a hyperlink
2331
--> $DIR/bare-urls.rs:9:5
2432
|
2533
LL | /// https://www.somewhere.com
26-
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://www.somewhere.com>`
34+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
2735
|
2836
= note: bare URLs are not automatically turned into clickable links
37+
help: use an automatic link instead
38+
|
39+
LL | /// <https://www.somewhere.com>
40+
| + +
2941

3042
error: this URL is not a hyperlink
3143
--> $DIR/bare-urls.rs:11:5
3244
|
3345
LL | /// https://www.somewhere.com/a
34-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://www.somewhere.com/a>`
46+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
3547
|
3648
= note: bare URLs are not automatically turned into clickable links
49+
help: use an automatic link instead
50+
|
51+
LL | /// <https://www.somewhere.com/a>
52+
| + +
3753

3854
error: this URL is not a hyperlink
3955
--> $DIR/bare-urls.rs:13:5
4056
|
4157
LL | /// https://subdomain.example.com
42-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://subdomain.example.com>`
58+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4359
|
4460
= note: bare URLs are not automatically turned into clickable links
61+
help: use an automatic link instead
62+
|
63+
LL | /// <https://subdomain.example.com>
64+
| + +
4565

4666
error: this URL is not a hyperlink
4767
--> $DIR/bare-urls.rs:15:5
4868
|
4969
LL | /// https://somewhere.com?
50-
| ^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://somewhere.com?>`
70+
| ^^^^^^^^^^^^^^^^^^^^^^
5171
|
5272
= note: bare URLs are not automatically turned into clickable links
73+
help: use an automatic link instead
74+
|
75+
LL | /// <https://somewhere.com?>
76+
| + +
5377

5478
error: this URL is not a hyperlink
5579
--> $DIR/bare-urls.rs:17:5
5680
|
5781
LL | /// https://somewhere.com/a?
58-
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://somewhere.com/a?>`
82+
| ^^^^^^^^^^^^^^^^^^^^^^^^
5983
|
6084
= note: bare URLs are not automatically turned into clickable links
85+
help: use an automatic link instead
86+
|
87+
LL | /// <https://somewhere.com/a?>
88+
| + +
6189

6290
error: this URL is not a hyperlink
6391
--> $DIR/bare-urls.rs:19:5
6492
|
6593
LL | /// https://somewhere.com?hello=12
66-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://somewhere.com?hello=12>`
94+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6795
|
6896
= note: bare URLs are not automatically turned into clickable links
97+
help: use an automatic link instead
98+
|
99+
LL | /// <https://somewhere.com?hello=12>
100+
| + +
69101

70102
error: this URL is not a hyperlink
71103
--> $DIR/bare-urls.rs:21:5
72104
|
73105
LL | /// https://somewhere.com/a?hello=12
74-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://somewhere.com/a?hello=12>`
106+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
75107
|
76108
= note: bare URLs are not automatically turned into clickable links
109+
help: use an automatic link instead
110+
|
111+
LL | /// <https://somewhere.com/a?hello=12>
112+
| + +
77113

78114
error: this URL is not a hyperlink
79115
--> $DIR/bare-urls.rs:23:5
80116
|
81117
LL | /// https://example.com?hello=12#xyz
82-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://example.com?hello=12#xyz>`
118+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
83119
|
84120
= note: bare URLs are not automatically turned into clickable links
121+
help: use an automatic link instead
122+
|
123+
LL | /// <https://example.com?hello=12#xyz>
124+
| + +
85125

86126
error: this URL is not a hyperlink
87127
--> $DIR/bare-urls.rs:25:5
88128
|
89129
LL | /// https://example.com/a?hello=12#xyz
90-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://example.com/a?hello=12#xyz>`
130+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
91131
|
92132
= note: bare URLs are not automatically turned into clickable links
133+
help: use an automatic link instead
134+
|
135+
LL | /// <https://example.com/a?hello=12#xyz>
136+
| + +
93137

94138
error: this URL is not a hyperlink
95139
--> $DIR/bare-urls.rs:27:5
96140
|
97141
LL | /// https://example.com#xyz
98-
| ^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://example.com#xyz>`
142+
| ^^^^^^^^^^^^^^^^^^^^^^^
99143
|
100144
= note: bare URLs are not automatically turned into clickable links
145+
help: use an automatic link instead
146+
|
147+
LL | /// <https://example.com#xyz>
148+
| + +
101149

102150
error: this URL is not a hyperlink
103151
--> $DIR/bare-urls.rs:29:5
104152
|
105153
LL | /// https://example.com/a#xyz
106-
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://example.com/a#xyz>`
154+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
107155
|
108156
= note: bare URLs are not automatically turned into clickable links
157+
help: use an automatic link instead
158+
|
159+
LL | /// <https://example.com/a#xyz>
160+
| + +
109161

110162
error: this URL is not a hyperlink
111163
--> $DIR/bare-urls.rs:31:5
112164
|
113165
LL | /// https://somewhere.com?hello=12&bye=11
114-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://somewhere.com?hello=12&bye=11>`
166+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
115167
|
116168
= note: bare URLs are not automatically turned into clickable links
169+
help: use an automatic link instead
170+
|
171+
LL | /// <https://somewhere.com?hello=12&bye=11>
172+
| + +
117173

118174
error: this URL is not a hyperlink
119175
--> $DIR/bare-urls.rs:33:5
120176
|
121177
LL | /// https://somewhere.com/a?hello=12&bye=11
122-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://somewhere.com/a?hello=12&bye=11>`
178+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
123179
|
124180
= note: bare URLs are not automatically turned into clickable links
181+
help: use an automatic link instead
182+
|
183+
LL | /// <https://somewhere.com/a?hello=12&bye=11>
184+
| + +
125185

126186
error: this URL is not a hyperlink
127187
--> $DIR/bare-urls.rs:35:5
128188
|
129189
LL | /// https://somewhere.com?hello=12&bye=11#xyz
130-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://somewhere.com?hello=12&bye=11#xyz>`
190+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
131191
|
132192
= note: bare URLs are not automatically turned into clickable links
193+
help: use an automatic link instead
194+
|
195+
LL | /// <https://somewhere.com?hello=12&bye=11#xyz>
196+
| + +
133197

134198
error: this URL is not a hyperlink
135199
--> $DIR/bare-urls.rs:37:10
136200
|
137201
LL | /// hey! https://somewhere.com/a?hello=12&bye=11#xyz
138-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<https://somewhere.com/a?hello=12&bye=11#xyz>`
202+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
139203
|
140204
= note: bare URLs are not automatically turned into clickable links
205+
help: use an automatic link instead
206+
|
207+
LL | /// hey! <https://somewhere.com/a?hello=12&bye=11#xyz>
208+
| + +
141209

142210
error: aborting due to 17 previous errors
143211

tests/rustdoc-ui/lints/renamed-lint-still-applies.stderr

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,18 @@ error: this URL is not a hyperlink
2929
--> $DIR/renamed-lint-still-applies.rs:9:5
3030
|
3131
LL | //! http://example.com
32-
| ^^^^^^^^^^^^^^^^^^ help: use an automatic link instead: `<http://example.com>`
32+
| ^^^^^^^^^^^^^^^^^^
3333
|
3434
= note: bare URLs are not automatically turned into clickable links
3535
note: the lint level is defined here
3636
--> $DIR/renamed-lint-still-applies.rs:7:9
3737
|
3838
LL | #![deny(rustdoc::non_autolinks)]
3939
| ^^^^^^^^^^^^^^^^^^^^^^
40+
help: use an automatic link instead
41+
|
42+
LL | //! <http://example.com>
43+
| + +
4044

4145
error: aborting due to 2 previous errors; 2 warnings emitted
4246

0 commit comments

Comments
 (0)