Skip to content

Commit

Permalink
Auto merge of #63703 - tommilligan:warn-empty-doctest, r=ollie27
Browse files Browse the repository at this point in the history
rustdoc: warn on empty doc test

Closes #60319.

A doc test that only contains whitespace should result in a warning.

This PR adds detection of empty doc tests to `check-code-block-syntax`, as having an invalid doc test is mutually exclusive with an empty doc test.
  • Loading branch information
bors committed Aug 31, 2019
2 parents fba38ac + 0ec2e9f commit 59cc53e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 10 deletions.
37 changes: 27 additions & 10 deletions src/librustdoc/passes/check_code_block_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -96,3 +108,8 @@ impl<'a, 'tcx> DocFolder for SyntaxChecker<'a, 'tcx> {
self.fold_item_recur(item)
}
}

enum CodeBlockInvalid {
SyntaxError,
Empty,
}
10 changes: 10 additions & 0 deletions src/test/rustdoc-ui/invalid-syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,13 @@ pub fn blargh() {}
/// \_
#[doc = "```"]
pub fn crazy_attrs() {}

/// ```rust
/// ```
pub fn empty_rust() {}

/// ```
///
///
/// ```
pub fn empty_rust_with_whitespace() {}
22 changes: 22 additions & 0 deletions src/test/rustdoc-ui/invalid-syntax.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,28 @@ LL | | #[doc = "```"]
|
= help: mark blocks that do not contain Rust code as text: ```text

warning: Rust code block is empty
--> $DIR/invalid-syntax.rs:68:5
|
LL | /// ```rust
| _____^
LL | | /// ```
| |_______^

warning: Rust code block is empty
--> $DIR/invalid-syntax.rs:72:5
|
LL | /// ```
| _____^
LL | | ///
LL | | ///
LL | | /// ```
| |_______^
help: mark blocks that do not contain Rust code as text
|
LL | /// ```text
| ^^^^^^^

error: unknown start of token: \
--> <rustdoc-highlighting>:1:1
|
Expand Down

0 comments on commit 59cc53e

Please sign in to comment.