Skip to content

Commit 2686daa

Browse files
Rollup merge of rust-lang#80580 - GuillaumeGomez:suggestion-ignore-codeblock-warn, r=jyn514
Add suggestion for "ignore" doc code block Part of rust-lang#30032. This PR adds a suggestion to help users when they have a "ignore" doc code block which is invalid rust code. r? `@jyn514`
2 parents bcd6975 + 7bc1eb4 commit 2686daa

File tree

4 files changed

+45
-6
lines changed

4 files changed

+45
-6
lines changed

src/librustdoc/html/markdown.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -1193,6 +1193,7 @@ crate struct RustCodeBlock {
11931193
crate code: Range<usize>,
11941194
crate is_fenced: bool,
11951195
crate syntax: Option<String>,
1196+
crate is_ignore: bool,
11961197
}
11971198

11981199
/// Returns a range of bytes for each code block in the markdown that is tagged as `rust` or
@@ -1208,7 +1209,7 @@ crate fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_, '_>) -> Vec<RustC
12081209

12091210
while let Some((event, offset)) = p.next() {
12101211
if let Event::Start(Tag::CodeBlock(syntax)) = event {
1211-
let (syntax, code_start, code_end, range, is_fenced) = match syntax {
1212+
let (syntax, code_start, code_end, range, is_fenced, is_ignore) = match syntax {
12121213
CodeBlockKind::Fenced(syntax) => {
12131214
let syntax = syntax.as_ref();
12141215
let lang_string = if syntax.is_empty() {
@@ -1219,6 +1220,7 @@ crate fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_, '_>) -> Vec<RustC
12191220
if !lang_string.rust {
12201221
continue;
12211222
}
1223+
let is_ignore = lang_string.ignore != Ignore::None;
12221224
let syntax = if syntax.is_empty() { None } else { Some(syntax.to_owned()) };
12231225
let (code_start, mut code_end) = match p.next() {
12241226
Some((Event::Text(_), offset)) => (offset.start, offset.end),
@@ -1229,6 +1231,7 @@ crate fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_, '_>) -> Vec<RustC
12291231
range: offset,
12301232
code,
12311233
syntax,
1234+
is_ignore,
12321235
});
12331236
continue;
12341237
}
@@ -1239,14 +1242,15 @@ crate fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_, '_>) -> Vec<RustC
12391242
range: offset,
12401243
code,
12411244
syntax,
1245+
is_ignore,
12421246
});
12431247
continue;
12441248
}
12451249
};
12461250
while let Some((Event::Text(_), offset)) = p.next() {
12471251
code_end = offset.end;
12481252
}
1249-
(syntax, code_start, code_end, offset, true)
1253+
(syntax, code_start, code_end, offset, true, is_ignore)
12501254
}
12511255
CodeBlockKind::Indented => {
12521256
// The ending of the offset goes too far sometime so we reduce it by one in
@@ -1258,9 +1262,10 @@ crate fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_, '_>) -> Vec<RustC
12581262
offset.end,
12591263
Range { start: offset.start, end: offset.end - 1 },
12601264
false,
1265+
false,
12611266
)
12621267
} else {
1263-
(None, offset.start, offset.end, offset, false)
1268+
(None, offset.start, offset.end, offset, false, false)
12641269
}
12651270
}
12661271
};
@@ -1270,6 +1275,7 @@ crate fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_, '_>) -> Vec<RustC
12701275
range,
12711276
code: Range { start: code_start, end: code_end },
12721277
syntax,
1278+
is_ignore,
12731279
});
12741280
}
12751281
}

src/librustdoc/passes/check_code_block_syntax.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
5151
let mut diag = if let Some(sp) =
5252
super::source_span_for_markdown_range(self.cx, &dox, &code_block.range, &item.attrs)
5353
{
54-
let warning_message = if buffer.has_errors {
55-
"could not parse code block as Rust code"
54+
let (warning_message, suggest_using_text) = if buffer.has_errors {
55+
("could not parse code block as Rust code", true)
5656
} else {
57-
"Rust code block is empty"
57+
("Rust code block is empty", false)
5858
};
5959

6060
let mut diag = self.cx.sess().struct_span_warn(sp, warning_message);
@@ -67,6 +67,15 @@ impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
6767
String::from("```text"),
6868
Applicability::MachineApplicable,
6969
);
70+
} else if suggest_using_text && code_block.is_ignore {
71+
let sp = sp.from_inner(InnerSpan::new(0, 3));
72+
diag.span_suggestion(
73+
sp,
74+
"`ignore` code blocks require valid Rust code for syntax highlighting. \
75+
Mark blocks that do not contain Rust code as text",
76+
String::from("```text,"),
77+
Applicability::MachineApplicable,
78+
);
7079
}
7180

7281
diag
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// check-pass
2+
3+
/// ```ignore (to-prevent-tidy-error)
4+
/// let heart = '❤️';
5+
/// ```
6+
//~^^^ WARN
7+
pub struct X;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
warning: could not parse code block as Rust code
2+
--> $DIR/ignore-block-help.rs:3:5
3+
|
4+
LL | /// ```ignore (to-prevent-tidy-error)
5+
| _____^
6+
LL | | /// let heart = '❤️';
7+
LL | | /// ```
8+
| |_______^
9+
|
10+
= note: error from rustc: character literal may only contain one codepoint
11+
help: `ignore` code blocks require valid Rust code for syntax highlighting. Mark blocks that do not contain Rust code as text
12+
|
13+
LL | /// ```text,ignore (to-prevent-tidy-error)
14+
| ^^^^^^^^
15+
16+
warning: 1 warning emitted
17+

0 commit comments

Comments
 (0)