Skip to content

Commit 5bf5b12

Browse files
Add suggestion in case a "ignore" doc block has invalid rust code inside
1 parent 75e1acb commit 5bf5b12

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

src/librustdoc/html/markdown.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,6 +1213,7 @@ crate struct RustCodeBlock {
12131213
crate code: Range<usize>,
12141214
crate is_fenced: bool,
12151215
crate syntax: Option<String>,
1216+
crate is_ignore: bool,
12161217
}
12171218

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

12291230
while let Some((event, offset)) = p.next() {
12301231
if let Event::Start(Tag::CodeBlock(syntax)) = event {
1231-
let (syntax, code_start, code_end, range, is_fenced) = match syntax {
1232+
let (syntax, code_start, code_end, range, is_fenced, is_ignore) = match syntax {
12321233
CodeBlockKind::Fenced(syntax) => {
12331234
let syntax = syntax.as_ref();
12341235
let lang_string = if syntax.is_empty() {
@@ -1249,6 +1250,7 @@ crate fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_, '_>) -> Vec<RustC
12491250
range: offset,
12501251
code,
12511252
syntax,
1253+
is_ignore: lang_string.ignore != Ignore::None,
12521254
});
12531255
continue;
12541256
}
@@ -1259,14 +1261,15 @@ crate fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_, '_>) -> Vec<RustC
12591261
range: offset,
12601262
code,
12611263
syntax,
1264+
is_ignore: lang_string.ignore != Ignore::None,
12621265
});
12631266
continue;
12641267
}
12651268
};
12661269
while let Some((Event::Text(_), offset)) = p.next() {
12671270
code_end = offset.end;
12681271
}
1269-
(syntax, code_start, code_end, offset, true)
1272+
(syntax, code_start, code_end, offset, true, lang_string.ignore != Ignore::None)
12701273
}
12711274
CodeBlockKind::Indented => {
12721275
// The ending of the offset goes too far sometime so we reduce it by one in
@@ -1278,9 +1281,10 @@ crate fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_, '_>) -> Vec<RustC
12781281
offset.end,
12791282
Range { start: offset.start, end: offset.end - 1 },
12801283
false,
1284+
false,
12811285
)
12821286
} else {
1283-
(None, offset.start, offset.end, offset, false)
1287+
(None, offset.start, offset.end, offset, false, false)
12841288
}
12851289
}
12861290
};
@@ -1290,6 +1294,7 @@ crate fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_, '_>) -> Vec<RustC
12901294
range,
12911295
code: Range { start: code_start, end: code_end },
12921296
syntax,
1297+
is_ignore,
12931298
});
12941299
}
12951300
}

src/librustdoc/passes/check_code_block_syntax.rs

Lines changed: 12 additions & 3 deletions
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

0 commit comments

Comments
 (0)