Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add suggestion for "ignore" doc code block #80580

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/librustdoc/html/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1213,6 +1213,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 @@ -1228,7 +1229,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 @@ -1239,6 +1240,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 @@ -1249,6 +1251,7 @@ crate fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_, '_>) -> Vec<RustC
range: offset,
code,
syntax,
is_ignore,
});
continue;
}
Expand All @@ -1259,14 +1262,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)
GuillaumeGomez marked this conversation as resolved.
Show resolved Hide resolved
(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 @@ -1278,9 +1282,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 @@ -1290,6 +1295,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