Rollup merge of #80580 - GuillaumeGomez:suggestion-ignore-codeblock-warn, r=jyn514
Add suggestion for "ignore" doc code block Part of https://github.com/rust-lang/rust/issues/30032. This PR adds a suggestion to help users when they have a "ignore" doc code block which is invalid rust code. r? `@jyn514`
This commit is contained in:
commit
2686daa779
@ -1193,6 +1193,7 @@ crate struct RustCodeBlock {
|
|||||||
crate code: Range<usize>,
|
crate code: Range<usize>,
|
||||||
crate is_fenced: bool,
|
crate is_fenced: bool,
|
||||||
crate syntax: Option<String>,
|
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
|
/// 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
|
|||||||
|
|
||||||
while let Some((event, offset)) = p.next() {
|
while let Some((event, offset)) = p.next() {
|
||||||
if let Event::Start(Tag::CodeBlock(syntax)) = event {
|
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) => {
|
CodeBlockKind::Fenced(syntax) => {
|
||||||
let syntax = syntax.as_ref();
|
let syntax = syntax.as_ref();
|
||||||
let lang_string = if syntax.is_empty() {
|
let lang_string = if syntax.is_empty() {
|
||||||
@ -1219,6 +1220,7 @@ crate fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_, '_>) -> Vec<RustC
|
|||||||
if !lang_string.rust {
|
if !lang_string.rust {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
let is_ignore = lang_string.ignore != Ignore::None;
|
||||||
let syntax = if syntax.is_empty() { None } else { Some(syntax.to_owned()) };
|
let syntax = if syntax.is_empty() { None } else { Some(syntax.to_owned()) };
|
||||||
let (code_start, mut code_end) = match p.next() {
|
let (code_start, mut code_end) = match p.next() {
|
||||||
Some((Event::Text(_), offset)) => (offset.start, offset.end),
|
Some((Event::Text(_), offset)) => (offset.start, offset.end),
|
||||||
@ -1229,6 +1231,7 @@ crate fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_, '_>) -> Vec<RustC
|
|||||||
range: offset,
|
range: offset,
|
||||||
code,
|
code,
|
||||||
syntax,
|
syntax,
|
||||||
|
is_ignore,
|
||||||
});
|
});
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -1239,6 +1242,7 @@ crate fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_, '_>) -> Vec<RustC
|
|||||||
range: offset,
|
range: offset,
|
||||||
code,
|
code,
|
||||||
syntax,
|
syntax,
|
||||||
|
is_ignore,
|
||||||
});
|
});
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -1246,7 +1250,7 @@ crate fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_, '_>) -> Vec<RustC
|
|||||||
while let Some((Event::Text(_), offset)) = p.next() {
|
while let Some((Event::Text(_), offset)) = p.next() {
|
||||||
code_end = offset.end;
|
code_end = offset.end;
|
||||||
}
|
}
|
||||||
(syntax, code_start, code_end, offset, true)
|
(syntax, code_start, code_end, offset, true, is_ignore)
|
||||||
}
|
}
|
||||||
CodeBlockKind::Indented => {
|
CodeBlockKind::Indented => {
|
||||||
// The ending of the offset goes too far sometime so we reduce it by one in
|
// 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
|
|||||||
offset.end,
|
offset.end,
|
||||||
Range { start: offset.start, end: offset.end - 1 },
|
Range { start: offset.start, end: offset.end - 1 },
|
||||||
false,
|
false,
|
||||||
|
false,
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
(None, offset.start, offset.end, offset, false)
|
(None, offset.start, offset.end, offset, false, false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -1270,6 +1275,7 @@ crate fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_, '_>) -> Vec<RustC
|
|||||||
range,
|
range,
|
||||||
code: Range { start: code_start, end: code_end },
|
code: Range { start: code_start, end: code_end },
|
||||||
syntax,
|
syntax,
|
||||||
|
is_ignore,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -51,10 +51,10 @@ impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
|
|||||||
let mut diag = if let Some(sp) =
|
let mut diag = if let Some(sp) =
|
||||||
super::source_span_for_markdown_range(self.cx, &dox, &code_block.range, &item.attrs)
|
super::source_span_for_markdown_range(self.cx, &dox, &code_block.range, &item.attrs)
|
||||||
{
|
{
|
||||||
let warning_message = if buffer.has_errors {
|
let (warning_message, suggest_using_text) = if buffer.has_errors {
|
||||||
"could not parse code block as Rust code"
|
("could not parse code block as Rust code", true)
|
||||||
} else {
|
} else {
|
||||||
"Rust code block is empty"
|
("Rust code block is empty", false)
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut diag = self.cx.sess().struct_span_warn(sp, warning_message);
|
let mut diag = self.cx.sess().struct_span_warn(sp, warning_message);
|
||||||
@ -67,6 +67,15 @@ impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
|
|||||||
String::from("```text"),
|
String::from("```text"),
|
||||||
Applicability::MachineApplicable,
|
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
|
diag
|
||||||
|
7
src/test/rustdoc-ui/ignore-block-help.rs
Normal file
7
src/test/rustdoc-ui/ignore-block-help.rs
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
// check-pass
|
||||||
|
|
||||||
|
/// ```ignore (to-prevent-tidy-error)
|
||||||
|
/// let heart = '❤️';
|
||||||
|
/// ```
|
||||||
|
//~^^^ WARN
|
||||||
|
pub struct X;
|
17
src/test/rustdoc-ui/ignore-block-help.stderr
Normal file
17
src/test/rustdoc-ui/ignore-block-help.stderr
Normal file
@ -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
|
||||||
|
|
Loading…
Reference in New Issue
Block a user