Skip to content

Commit 4120f75

Browse files
committed
Add back missing help for ignore blocks
This also gives a better error message when a span is missing.
1 parent b574c67 commit 4120f75

File tree

6 files changed

+40
-34
lines changed

6 files changed

+40
-34
lines changed

src/doc/rustdoc/src/lints.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ warning: Rust code block is empty
328328
--> src/lib.rs:8:5
329329
|
330330
8 | /// ```rust
331-
| ^^^^^^^
331+
| ^^^^^^^
332332
```
333333

334334
## bare_urls

src/librustdoc/passes/check_code_block_syntax.rs

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
6161
};
6262

6363
let hir_id = self.cx.tcx.hir().local_def_id_to_hir_id(local_id);
64-
let suggest_using_text = code_block.syntax.is_none() && code_block.is_fenced;
64+
let empty_block = code_block.syntax.is_none() && code_block.is_fenced;
6565
let is_ignore = code_block.is_ignore;
6666

6767
// The span and whether it is precise or not.
@@ -78,40 +78,38 @@ impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
7878
// lambda that will use the lint to start a new diagnostic and add
7979
// a suggestion to it when needed.
8080
let diag_builder = |lint: LintDiagnosticBuilder<'_>| {
81-
let mut diag = if precise_span {
82-
let msg = if buffer.has_errors {
83-
"could not parse code block as Rust code"
84-
} else {
85-
"Rust code block is empty"
86-
};
87-
88-
let mut diag = lint.build(msg);
89-
90-
if suggest_using_text {
91-
let extended_msg = if is_ignore {
92-
"`ignore` code blocks require valid Rust code for syntax highlighting. \
93-
Mark blocks that do not contain Rust code as text"
94-
} else {
95-
"mark blocks that do not contain Rust code as text"
96-
};
81+
let explanation = if is_ignore {
82+
"`ignore` code blocks require valid Rust code for syntax highlighting; \
83+
mark blocks that do not contain Rust code as text"
84+
} else {
85+
"mark blocks that do not contain Rust code as text"
86+
};
87+
let msg = if buffer.has_errors {
88+
"could not parse code block as Rust code"
89+
} else {
90+
"Rust code block is empty"
91+
};
92+
let mut diag = lint.build(msg);
9793

94+
if precise_span {
95+
if is_ignore {
96+
// giving an accurate suggestion is hard because `ignore` might not have come first in the list.
97+
// just give a `help` instead.
98+
diag.span_help(
99+
sp.from_inner(InnerSpan::new(0, 3)),
100+
&format!("{}: ```text", explanation),
101+
);
102+
} else if empty_block {
98103
diag.span_suggestion(
99104
sp.from_inner(InnerSpan::new(0, 3)),
100-
extended_msg,
105+
explanation,
101106
String::from("```text"),
102107
Applicability::MachineApplicable,
103108
);
104109
}
105-
106-
diag
107-
} else {
108-
let mut diag = lint.build("doc comment contains an invalid Rust code block");
109-
if suggest_using_text {
110-
diag.help("mark blocks that do not contain Rust code as text: ```text");
111-
}
112-
113-
diag
114-
};
110+
} else if empty_block || is_ignore {
111+
diag.help(&format!("{}: ```text", explanation));
112+
}
115113

116114
// FIXME(#67563): Provide more context for these errors by displaying the spans inline.
117115
for message in buffer.messages.iter() {

src/test/rustdoc-ui/ignore-block-help.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,8 @@
33
/// ```ignore (to-prevent-tidy-error)
44
/// let heart = '❤️';
55
/// ```
6-
//~^^^ WARN
6+
//~^^^ WARNING could not parse code block
7+
//~| NOTE on by default
8+
//~| NOTE character literal may only contain one codepoint
9+
//~| HELP `ignore` code blocks require valid Rust code
710
pub struct X;

src/test/rustdoc-ui/ignore-block-help.stderr

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ LL | | /// let heart = '❤️';
77
LL | | /// ```
88
| |_______^
99
|
10-
= note: `#[warn(invalid_rust_codeblock)]` on by default
10+
= note: `#[warn(rustdoc::invalid_rust_codeblock)]` on by default
11+
help: `ignore` code blocks require valid Rust code for syntax highlighting; mark blocks that do not contain Rust code as text: ```text
12+
--> $DIR/ignore-block-help.rs:3:5
13+
|
14+
LL | /// ```ignore (to-prevent-tidy-error)
15+
| ^^^
1116
= note: error from rustc: character literal may only contain one codepoint
1217

1318
warning: 1 warning emitted

src/test/rustdoc-ui/invalid-syntax.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pub fn blargh() {}
7171
/// \_
7272
#[doc = "```"]
7373
pub fn crazy_attrs() {}
74-
//~^^^^ WARNING doc comment contains an invalid Rust code block
74+
//~^^^^ WARNING could not parse code block
7575

7676
/// ```rust
7777
/// ```

src/test/rustdoc-ui/invalid-syntax.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | | /// \__________pkt->size___________/ \_result->size_/ \__pkt->si
77
LL | | /// ```
88
| |_______^
99
|
10-
= note: `#[warn(invalid_rust_codeblock)]` on by default
10+
= note: `#[warn(rustdoc::invalid_rust_codeblock)]` on by default
1111
= note: error from rustc: unknown start of token: \
1212
= note: error from rustc: unknown start of token: \
1313
= note: error from rustc: unknown start of token: \
@@ -91,7 +91,7 @@ LL | | /// ```
9191
|
9292
= note: error from rustc: unknown start of token: \
9393

94-
warning: doc comment contains an invalid Rust code block
94+
warning: could not parse code block as Rust code
9595
--> $DIR/invalid-syntax.rs:70:1
9696
|
9797
LL | / #[doc = "```"]

0 commit comments

Comments
 (0)