diff options
| author | Tom Milligan <tom@reinfer.io> | 2019-08-19 15:19:26 +0100 |
|---|---|---|
| committer | Tom Milligan <tom@reinfer.io> | 2019-08-19 16:02:57 +0100 |
| commit | 0ec2e9fcebd18d98a32884786e1c4bbaf3db1ce0 (patch) | |
| tree | 847f6de824c84e35a3c367e6dfb1b1bb30730a0b /src/librustdoc/passes/check_code_block_syntax.rs | |
| parent | ea52be482ab4945fda63cb65b6a198309a041e3c (diff) | |
| download | rust-0ec2e9fcebd18d98a32884786e1c4bbaf3db1ce0.tar.gz rust-0ec2e9fcebd18d98a32884786e1c4bbaf3db1ce0.zip | |
librustdoc: warn on empty doc test
Diffstat (limited to 'src/librustdoc/passes/check_code_block_syntax.rs')
| -rw-r--r-- | src/librustdoc/passes/check_code_block_syntax.rs | 37 |
1 files changed, 27 insertions, 10 deletions
diff --git a/src/librustdoc/passes/check_code_block_syntax.rs b/src/librustdoc/passes/check_code_block_syntax.rs index 357e17d2d1b..5c4159433c7 100644 --- a/src/librustdoc/passes/check_code_block_syntax.rs +++ b/src/librustdoc/passes/check_code_block_syntax.rs @@ -32,27 +32,39 @@ impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> { dox[code_block.code].to_owned(), ); - let has_errors = { - let mut has_errors = false; + let validation_status = { + let mut has_syntax_errors = false; + let mut only_whitespace = true; + // even if there is a syntax error, we need to run the lexer over the whole file let mut lexer = Lexer::new(&sess, source_file, None); loop { match lexer.next_token().kind { token::Eof => break, - token::Unknown(..) => has_errors = true, - _ => (), + token::Whitespace => (), + token::Unknown(..) => has_syntax_errors = true, + _ => only_whitespace = false, } } - has_errors + + if has_syntax_errors { + Some(CodeBlockInvalid::SyntaxError) + } else if only_whitespace { + Some(CodeBlockInvalid::Empty) + } else { + None + } }; - if has_errors { + if let Some(code_block_invalid) = validation_status { let mut diag = if let Some(sp) = super::source_span_for_markdown_range(self.cx, &dox, &code_block.range, &item.attrs) { - let mut diag = self - .cx - .sess() - .struct_span_warn(sp, "could not parse code block as Rust code"); + let warning_message = match code_block_invalid { + CodeBlockInvalid::SyntaxError => "could not parse code block as Rust code", + CodeBlockInvalid::Empty => "Rust code block is empty", + }; + + let mut diag = self.cx.sess().struct_span_warn(sp, warning_message); if code_block.syntax.is_none() && code_block.is_fenced { let sp = sp.from_inner(InnerSpan::new(0, 3)); @@ -96,3 +108,8 @@ impl<'a, 'tcx> DocFolder for SyntaxChecker<'a, 'tcx> { self.fold_item_recur(item) } } + +enum CodeBlockInvalid { + SyntaxError, + Empty, +} |
