Skip to content

Commit e8550e1

Browse files
committed
Adjust rustdoc automatic link suggestion
Use more accurate spans for multipart suggestion.
1 parent ba252ed commit e8550e1

File tree

5 files changed

+37
-39
lines changed

5 files changed

+37
-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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ LL | ...ny(rustdoc::bare_url...
1313
help: use an automatic link instead
1414
|
1515
LL | /// This is a long line that contains a <http://link.com>
16-
| ~~~~~~~~~~~~~~~~~
16+
| + +
1717

1818
error: aborting due to 1 previous error
1919

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ LL | #![deny(rustdoc::bare_urls)]
1313
help: use an automatic link instead
1414
|
1515
LL | HEADS UP! <https://example.com> MUST SHOW UP IN THE STDERR FILE!
16-
| ~~~~~~~~~~~~~~~~~~~~~
16+
| + +
1717

1818
error: aborting due to 1 previous error
1919

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ LL | #![deny(rustdoc::bare_urls)]
1313
help: use an automatic link instead
1414
|
1515
LL | /// <https://somewhere.com>
16-
| ~~~~~~~~~~~~~~~~~~~~~~~
16+
| + +
1717

1818
error: this URL is not a hyperlink
1919
--> $DIR/bare-urls.rs:7:5
@@ -25,7 +25,7 @@ LL | /// https://somewhere.com/a
2525
help: use an automatic link instead
2626
|
2727
LL | /// <https://somewhere.com/a>
28-
| ~~~~~~~~~~~~~~~~~~~~~~~~~
28+
| + +
2929

3030
error: this URL is not a hyperlink
3131
--> $DIR/bare-urls.rs:9:5
@@ -37,7 +37,7 @@ LL | /// https://www.somewhere.com
3737
help: use an automatic link instead
3838
|
3939
LL | /// <https://www.somewhere.com>
40-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~
40+
| + +
4141

4242
error: this URL is not a hyperlink
4343
--> $DIR/bare-urls.rs:11:5
@@ -49,7 +49,7 @@ LL | /// https://www.somewhere.com/a
4949
help: use an automatic link instead
5050
|
5151
LL | /// <https://www.somewhere.com/a>
52-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
52+
| + +
5353

5454
error: this URL is not a hyperlink
5555
--> $DIR/bare-urls.rs:13:5
@@ -61,7 +61,7 @@ LL | /// https://subdomain.example.com
6161
help: use an automatic link instead
6262
|
6363
LL | /// <https://subdomain.example.com>
64-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
64+
| + +
6565

6666
error: this URL is not a hyperlink
6767
--> $DIR/bare-urls.rs:15:5
@@ -73,7 +73,7 @@ LL | /// https://somewhere.com?
7373
help: use an automatic link instead
7474
|
7575
LL | /// <https://somewhere.com?>
76-
| ~~~~~~~~~~~~~~~~~~~~~~~~
76+
| + +
7777

7878
error: this URL is not a hyperlink
7979
--> $DIR/bare-urls.rs:17:5
@@ -85,7 +85,7 @@ LL | /// https://somewhere.com/a?
8585
help: use an automatic link instead
8686
|
8787
LL | /// <https://somewhere.com/a?>
88-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~
88+
| + +
8989

9090
error: this URL is not a hyperlink
9191
--> $DIR/bare-urls.rs:19:5
@@ -97,7 +97,7 @@ LL | /// https://somewhere.com?hello=12
9797
help: use an automatic link instead
9898
|
9999
LL | /// <https://somewhere.com?hello=12>
100-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
100+
| + +
101101

102102
error: this URL is not a hyperlink
103103
--> $DIR/bare-urls.rs:21:5
@@ -109,7 +109,7 @@ LL | /// https://somewhere.com/a?hello=12
109109
help: use an automatic link instead
110110
|
111111
LL | /// <https://somewhere.com/a?hello=12>
112-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
112+
| + +
113113

114114
error: this URL is not a hyperlink
115115
--> $DIR/bare-urls.rs:23:5
@@ -121,7 +121,7 @@ LL | /// https://example.com?hello=12#xyz
121121
help: use an automatic link instead
122122
|
123123
LL | /// <https://example.com?hello=12#xyz>
124-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
124+
| + +
125125

126126
error: this URL is not a hyperlink
127127
--> $DIR/bare-urls.rs:25:5
@@ -133,7 +133,7 @@ LL | /// https://example.com/a?hello=12#xyz
133133
help: use an automatic link instead
134134
|
135135
LL | /// <https://example.com/a?hello=12#xyz>
136-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
136+
| + +
137137

138138
error: this URL is not a hyperlink
139139
--> $DIR/bare-urls.rs:27:5
@@ -145,7 +145,7 @@ LL | /// https://example.com#xyz
145145
help: use an automatic link instead
146146
|
147147
LL | /// <https://example.com#xyz>
148-
| ~~~~~~~~~~~~~~~~~~~~~~~~~
148+
| + +
149149

150150
error: this URL is not a hyperlink
151151
--> $DIR/bare-urls.rs:29:5
@@ -157,7 +157,7 @@ LL | /// https://example.com/a#xyz
157157
help: use an automatic link instead
158158
|
159159
LL | /// <https://example.com/a#xyz>
160-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~
160+
| + +
161161

162162
error: this URL is not a hyperlink
163163
--> $DIR/bare-urls.rs:31:5
@@ -169,7 +169,7 @@ LL | /// https://somewhere.com?hello=12&bye=11
169169
help: use an automatic link instead
170170
|
171171
LL | /// <https://somewhere.com?hello=12&bye=11>
172-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
172+
| + +
173173

174174
error: this URL is not a hyperlink
175175
--> $DIR/bare-urls.rs:33:5
@@ -181,7 +181,7 @@ LL | /// https://somewhere.com/a?hello=12&bye=11
181181
help: use an automatic link instead
182182
|
183183
LL | /// <https://somewhere.com/a?hello=12&bye=11>
184-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
184+
| + +
185185

186186
error: this URL is not a hyperlink
187187
--> $DIR/bare-urls.rs:35:5
@@ -193,7 +193,7 @@ LL | /// https://somewhere.com?hello=12&bye=11#xyz
193193
help: use an automatic link instead
194194
|
195195
LL | /// <https://somewhere.com?hello=12&bye=11#xyz>
196-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
196+
| + +
197197

198198
error: this URL is not a hyperlink
199199
--> $DIR/bare-urls.rs:37:10
@@ -205,7 +205,7 @@ LL | /// hey! https://somewhere.com/a?hello=12&bye=11#xyz
205205
help: use an automatic link instead
206206
|
207207
LL | /// hey! <https://somewhere.com/a?hello=12&bye=11#xyz>
208-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
208+
| + +
209209

210210
error: aborting due to 17 previous errors
211211

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ LL | #![deny(rustdoc::non_autolinks)]
4040
help: use an automatic link instead
4141
|
4242
LL | //! <http://example.com>
43-
| ~~~~~~~~~~~~~~~~~~~~
43+
| + +
4444

4545
error: aborting due to 2 previous errors; 2 warnings emitted
4646

0 commit comments

Comments
 (0)