Skip to content

Commit 76b5b27

Browse files
committed
Use multipart suggestion for code wrapping
Another one of those "good grief, I just submitted it and NOW I think of it" moments.
1 parent 0db9e40 commit 76b5b27

File tree

4 files changed

+50
-52
lines changed

4 files changed

+50
-52
lines changed

src/librustdoc/passes/html_tags.rs

+10-17
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ impl<'a, 'tcx> DocVisitor for InvalidHtmlTagsLinter<'a, 'tcx> {
220220
// and we don't try to detect stuff `<like this>` because that's not valid Rust.
221221
if let Some(Some(generics_start)) = (is_open_tag
222222
&& dox[..range.end].ends_with(">"))
223-
.then(|| extract_path_backwards(&dox, range.start))
223+
.then(|| extract_path_backwards(&dox, range.start))
224224
{
225225
let generics_sp = match super::source_span_for_markdown_range(
226226
tcx,
@@ -231,22 +231,15 @@ impl<'a, 'tcx> DocVisitor for InvalidHtmlTagsLinter<'a, 'tcx> {
231231
Some(sp) => sp,
232232
None => item.attr_span(tcx),
233233
};
234-
if let Ok(generics_snippet) =
235-
tcx.sess.source_map().span_to_snippet(generics_sp)
236-
{
237-
// short form is chosen here because ``Vec<i32>`` would be confusing.
238-
diag.span_suggestion_short(
239-
generics_sp,
240-
"try marking as source code with `backticks`",
241-
format!("`{}`", generics_snippet),
242-
Applicability::MaybeIncorrect,
243-
);
244-
} else {
245-
diag.span_help(
246-
generics_sp,
247-
"try marking as source code with `backticks`",
248-
);
249-
}
234+
// multipart form is chosen here because ``Vec<i32>`` would be confusing.
235+
diag.multipart_suggestion(
236+
"try marking as source code",
237+
vec![
238+
(generics_sp.shrink_to_lo(), String::from("`")),
239+
(generics_sp.shrink_to_hi(), String::from("`")),
240+
],
241+
Applicability::MaybeIncorrect,
242+
);
250243
}
251244
diag.emit()
252245
});

src/test/rustdoc-ui/suggestions/html-as-generics.fixed

-6
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,29 @@
44
/// This `Vec<i32>` thing!
55
//~^ERROR unclosed HTML tag `i32`
66
//~|HELP try marking as source
7-
//~|SUGGESTION `Vec<i32>`
87
pub struct Generic;
98

109
/// This `vec::Vec<i32>` thing!
1110
//~^ERROR unclosed HTML tag `i32`
1211
//~|HELP try marking as source
13-
//~|SUGGESTION `vec::Vec<i32>`
1412
pub struct GenericPath;
1513

1614
/// This `i32<i32>` thing!
1715
//~^ERROR unclosed HTML tag `i32`
1816
//~|HELP try marking as source
19-
//~|SUGGESTION `i32<i32>`
2017
pub struct PathsCanContainTrailingNumbers;
2118

2219
/// This `Vec::<i32>` thing!
2320
//~^ERROR unclosed HTML tag `i32`
2421
//~|HELP try marking as source
25-
//~|SUGGESTION `Vec::<i32>`
2622
pub struct Turbofish;
2723

2824
/// This [link](https://rust-lang.org)`::<i32>` thing!
2925
//~^ERROR unclosed HTML tag `i32`
3026
//~|HELP try marking as source
31-
//~|SUGGESTION `::<i32>`
3227
pub struct BareTurbofish;
3328

3429
/// This <span>`Vec::<i32>`</span> thing!
3530
//~^ERROR unclosed HTML tag `i32`
3631
//~|HELP try marking as source
37-
//~|SUGGESTION `Vec::<i32>`
3832
pub struct Nested;

src/test/rustdoc-ui/suggestions/html-as-generics.rs

-6
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,29 @@
44
/// This Vec<i32> thing!
55
//~^ERROR unclosed HTML tag `i32`
66
//~|HELP try marking as source
7-
//~|SUGGESTION `Vec<i32>`
87
pub struct Generic;
98

109
/// This vec::Vec<i32> thing!
1110
//~^ERROR unclosed HTML tag `i32`
1211
//~|HELP try marking as source
13-
//~|SUGGESTION `vec::Vec<i32>`
1412
pub struct GenericPath;
1513

1614
/// This i32<i32> thing!
1715
//~^ERROR unclosed HTML tag `i32`
1816
//~|HELP try marking as source
19-
//~|SUGGESTION `i32<i32>`
2017
pub struct PathsCanContainTrailingNumbers;
2118

2219
/// This Vec::<i32> thing!
2320
//~^ERROR unclosed HTML tag `i32`
2421
//~|HELP try marking as source
25-
//~|SUGGESTION `Vec::<i32>`
2622
pub struct Turbofish;
2723

2824
/// This [link](https://rust-lang.org)::<i32> thing!
2925
//~^ERROR unclosed HTML tag `i32`
3026
//~|HELP try marking as source
31-
//~|SUGGESTION `::<i32>`
3227
pub struct BareTurbofish;
3328

3429
/// This <span>Vec::<i32></span> thing!
3530
//~^ERROR unclosed HTML tag `i32`
3631
//~|HELP try marking as source
37-
//~|SUGGESTION `Vec::<i32>`
3832
pub struct Nested;

src/test/rustdoc-ui/suggestions/html-as-generics.stderr

+40-23
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,72 @@ error: unclosed HTML tag `i32`
22
--> $DIR/html-as-generics.rs:4:13
33
|
44
LL | /// This Vec<i32> thing!
5-
| ---^^^^^
6-
| |
7-
| help: try marking as source code with `backticks`
5+
| ^^^^^
86
|
97
note: the lint level is defined here
108
--> $DIR/html-as-generics.rs:2:9
119
|
1210
LL | #![deny(rustdoc::invalid_html_tags)]
1311
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
help: try marking as source code
13+
|
14+
LL | /// This `Vec<i32>` thing!
15+
| + +
1416

1517
error: unclosed HTML tag `i32`
16-
--> $DIR/html-as-generics.rs:10:18
18+
--> $DIR/html-as-generics.rs:9:18
1719
|
1820
LL | /// This vec::Vec<i32> thing!
19-
| --------^^^^^
20-
| |
21-
| help: try marking as source code with `backticks`
21+
| ^^^^^
22+
|
23+
help: try marking as source code
24+
|
25+
LL | /// This `vec::Vec<i32>` thing!
26+
| + +
2227

2328
error: unclosed HTML tag `i32`
24-
--> $DIR/html-as-generics.rs:16:13
29+
--> $DIR/html-as-generics.rs:14:13
2530
|
2631
LL | /// This i32<i32> thing!
27-
| ---^^^^^
28-
| |
29-
| help: try marking as source code with `backticks`
32+
| ^^^^^
33+
|
34+
help: try marking as source code
35+
|
36+
LL | /// This `i32<i32>` thing!
37+
| + +
3038

3139
error: unclosed HTML tag `i32`
32-
--> $DIR/html-as-generics.rs:22:15
40+
--> $DIR/html-as-generics.rs:19:15
3341
|
3442
LL | /// This Vec::<i32> thing!
35-
| -----^^^^^
36-
| |
37-
| help: try marking as source code with `backticks`
43+
| ^^^^^
44+
|
45+
help: try marking as source code
46+
|
47+
LL | /// This `Vec::<i32>` thing!
48+
| + +
3849

3950
error: unclosed HTML tag `i32`
40-
--> $DIR/html-as-generics.rs:28:41
51+
--> $DIR/html-as-generics.rs:24:41
4152
|
4253
LL | /// This [link](https://rust-lang.org)::<i32> thing!
43-
| --^^^^^
44-
| |
45-
| help: try marking as source code with `backticks`
54+
| ^^^^^
55+
|
56+
help: try marking as source code
57+
|
58+
LL | /// This [link](https://rust-lang.org)`::<i32>` thing!
59+
| + +
4660

4761
error: unclosed HTML tag `i32`
48-
--> $DIR/html-as-generics.rs:34:21
62+
--> $DIR/html-as-generics.rs:29:21
4963
|
5064
LL | /// This <span>Vec::<i32></span> thing!
51-
| -----^^^^^
52-
| |
53-
| help: try marking as source code with `backticks`
65+
| ^^^^^
66+
|
67+
help: try marking as source code
68+
|
69+
LL | /// This <span>`Vec::<i32>`</span> thing!
70+
| + +
5471

5572
error: aborting due to 6 previous errors
5673

0 commit comments

Comments
 (0)