From 5bf5b127070b510dee82770b921aa39137c1d889 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 1 Jan 2021 15:35:12 +0100 Subject: [PATCH 1/3] Add suggestion in case a "ignore" doc block has invalid rust code inside --- src/librustdoc/html/markdown.rs | 11 ++++++++--- src/librustdoc/passes/check_code_block_syntax.rs | 15 ++++++++++++--- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs index 6261e843d23..f1d00b81eed 100644 --- a/src/librustdoc/html/markdown.rs +++ b/src/librustdoc/html/markdown.rs @@ -1213,6 +1213,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 @@ -1228,7 +1229,7 @@ crate fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_, '_>) -> Vec { let syntax = syntax.as_ref(); let lang_string = if syntax.is_empty() { @@ -1249,6 +1250,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 @@ -1278,9 +1281,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 From c3ae82690150a9edc09ceef6319eabedfebf3931 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 1 Jan 2021 16:05:36 +0100 Subject: [PATCH 2/3] Add test for suggestion in case a "ignore" doc block has invalid rust code inside --- src/test/rustdoc-ui/ignore-block-help.rs | 7 +++++++ src/test/rustdoc-ui/ignore-block-help.stderr | 17 +++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 src/test/rustdoc-ui/ignore-block-help.rs create mode 100644 src/test/rustdoc-ui/ignore-block-help.stderr 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 + From 7bc1eb4506170e0750de4757af97f341c427a35a Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sun, 3 Jan 2021 14:56:56 +0100 Subject: [PATCH 3/3] Create a "is_ignore" variable instead of doing the comparison multiple times --- src/librustdoc/html/markdown.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs index f1d00b81eed..291a01d7468 100644 --- a/src/librustdoc/html/markdown.rs +++ b/src/librustdoc/html/markdown.rs @@ -1240,6 +1240,7 @@ crate fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_, '_>) -> Vec (offset.start, offset.end), @@ -1250,7 +1251,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