diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs index 07744139605..0a8c89f6caf 100644 --- a/src/librustdoc/html/markdown.rs +++ b/src/librustdoc/html/markdown.rs @@ -1193,6 +1193,7 @@ crate struct RustCodeBlock { crate code: Range, crate is_fenced: bool, crate syntax: Option, + crate is_ignore: bool, } /// 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 { let syntax = syntax.as_ref(); let lang_string = if syntax.is_empty() { @@ -1219,6 +1220,7 @@ crate fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_, '_>) -> Vec (offset.start, offset.end), @@ -1229,6 +1231,7 @@ crate fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_, '_>) -> Vec) -> Vec) -> Vec { // 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) -> Vec 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); @@ -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 diff --git a/src/test/rustdoc-ui/ignore-block-help.rs b/src/test/rustdoc-ui/ignore-block-help.rs new file mode 100644 index 00000000000..c22dddd11df --- /dev/null +++ b/src/test/rustdoc-ui/ignore-block-help.rs @@ -0,0 +1,7 @@ +// check-pass + +/// ```ignore (to-prevent-tidy-error) +/// let heart = '❤️'; +/// ``` +//~^^^ WARN +pub struct X; diff --git a/src/test/rustdoc-ui/ignore-block-help.stderr b/src/test/rustdoc-ui/ignore-block-help.stderr new file mode 100644 index 00000000000..d45cd92d2d1 --- /dev/null +++ b/src/test/rustdoc-ui/ignore-block-help.stderr @@ -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 +