Skip to content

Commit

Permalink
Rollup merge of rust-lang#80580 - GuillaumeGomez:suggestion-ignore-co…
Browse files Browse the repository at this point in the history
…deblock-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`
  • Loading branch information
GuillaumeGomez authored Jan 3, 2021
2 parents bcd6975 + 7bc1eb4 commit 2686daa
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 6 deletions.
12 changes: 9 additions & 3 deletions src/librustdoc/html/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1193,6 +1193,7 @@ crate struct RustCodeBlock {
crate code: Range<usize>,
crate is_fenced: bool,
crate syntax: Option<String>,
crate is_ignore: bool,
}

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

while let Some((event, offset)) = p.next() {
if let Event::Start(Tag::CodeBlock(syntax)) = event {
let (syntax, code_start, code_end, range, is_fenced) = match syntax {
let (syntax, code_start, code_end, range, is_fenced, is_ignore) = match syntax {
CodeBlockKind::Fenced(syntax) => {
let syntax = syntax.as_ref();
let lang_string = if syntax.is_empty() {
Expand All @@ -1219,6 +1220,7 @@ crate fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_, '_>) -> Vec<RustC
if !lang_string.rust {
continue;
}
let is_ignore = lang_string.ignore != Ignore::None;
let syntax = if syntax.is_empty() { None } else { Some(syntax.to_owned()) };
let (code_start, mut code_end) = match p.next() {
Some((Event::Text(_), offset)) => (offset.start, offset.end),
Expand All @@ -1229,6 +1231,7 @@ crate fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_, '_>) -> Vec<RustC
range: offset,
code,
syntax,
is_ignore,
});
continue;
}
Expand All @@ -1239,14 +1242,15 @@ crate fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_, '_>) -> Vec<RustC
range: offset,
code,
syntax,
is_ignore,
});
continue;
}
};
while let Some((Event::Text(_), offset)) = p.next() {
code_end = offset.end;
}
(syntax, code_start, code_end, offset, true)
(syntax, code_start, code_end, offset, true, is_ignore)
}
CodeBlockKind::Indented => {
// The ending of the offset goes too far sometime so we reduce it by one in
Expand All @@ -1258,9 +1262,10 @@ crate fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_, '_>) -> Vec<RustC
offset.end,
Range { start: offset.start, end: offset.end - 1 },
false,
false,
)
} else {
(None, offset.start, offset.end, offset, false)
(None, offset.start, offset.end, offset, false, false)
}
}
};
Expand All @@ -1270,6 +1275,7 @@ crate fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_, '_>) -> Vec<RustC
range,
code: Range { start: code_start, end: code_end },
syntax,
is_ignore,
});
}
}
Expand Down
15 changes: 12 additions & 3 deletions src/librustdoc/passes/check_code_block_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
let mut diag = if let Some(sp) =
super::source_span_for_markdown_range(self.cx, &dox, &code_block.range, &item.attrs)
{
let warning_message = if buffer.has_errors {
"could not parse code block as Rust code"
let (warning_message, suggest_using_text) = if buffer.has_errors {
("could not parse code block as Rust code", true)
} else {
"Rust code block is empty"
("Rust code block is empty", false)
};

let mut diag = self.cx.sess().struct_span_warn(sp, warning_message);
Expand All @@ -67,6 +67,15 @@ impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
String::from("```text"),
Applicability::MachineApplicable,
);
} else if suggest_using_text && code_block.is_ignore {
let sp = sp.from_inner(InnerSpan::new(0, 3));
diag.span_suggestion(
sp,
"`ignore` code blocks require valid Rust code for syntax highlighting. \
Mark blocks that do not contain Rust code as text",
String::from("```text,"),
Applicability::MachineApplicable,
);
}

diag
Expand Down
7 changes: 7 additions & 0 deletions src/test/rustdoc-ui/ignore-block-help.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// check-pass

/// ```ignore (to-prevent-tidy-error)
/// let heart = '❤️';
/// ```
//~^^^ WARN
pub struct X;
17 changes: 17 additions & 0 deletions src/test/rustdoc-ui/ignore-block-help.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
warning: could not parse code block as Rust code
--> $DIR/ignore-block-help.rs:3:5
|
LL | /// ```ignore (to-prevent-tidy-error)
| _____^
LL | | /// let heart = '❤️';
LL | | /// ```
| |_______^
|
= note: error from rustc: character literal may only contain one codepoint
help: `ignore` code blocks require valid Rust code for syntax highlighting. Mark blocks that do not contain Rust code as text
|
LL | /// ```text,ignore (to-prevent-tidy-error)
| ^^^^^^^^

warning: 1 warning emitted

0 comments on commit 2686daa

Please sign in to comment.