Skip to content

Throw errors when doc comments are added where they're unused #43009

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 30, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Make a lint instead
  • Loading branch information
GuillaumeGomez committed Jul 27, 2017
commit 1cebf98e4c2654548e764e937e0b712220ffb600
7 changes: 7 additions & 0 deletions src/librustc/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,13 @@ impl Decl_ {
DeclItem(_) => &[]
}
}

pub fn is_local(&self) -> bool {
match *self {
Decl_::DeclLocal(_) => true,
_ => false,
}
}
}

/// represents one arm of a 'match'
Expand Down
40 changes: 40 additions & 0 deletions src/librustc_lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,46 @@ impl EarlyLintPass for IllegalFloatLiteralPattern {
}
}

declare_lint! {
pub UNUSED_DOC_COMMENT,
Warn,
"detects doc comments that aren't used by rustdoc"
}

#[derive(Copy, Clone)]
pub struct UnusedDocComment;

impl LintPass for UnusedDocComment {
fn get_lints(&self) -> LintArray {
lint_array![UNUSED_DOC_COMMENT]
}
}

impl UnusedDocComment {
fn warn_if_doc<'a, 'tcx,
I: Iterator<Item=&'a ast::Attribute>,
C: LintContext<'tcx>>(&self, mut attrs: I, cx: &C) {
if let Some(attr) = attrs.find(|a| a.is_value_str() && a.check_name("doc")) {
cx.struct_span_lint(UNUSED_DOC_COMMENT, attr.span, "doc comment not used by rustdoc")
.emit();
}
}
}

impl EarlyLintPass for UnusedDocComment {
fn check_local(&mut self, cx: &EarlyContext, decl: &ast::Local) {
self.warn_if_doc(decl.attrs.iter(), cx);
}

fn check_arm(&mut self, cx: &EarlyContext, arm: &ast::Arm) {
self.warn_if_doc(arm.attrs.iter(), cx);
}

fn check_expr(&mut self, cx: &EarlyContext, expr: &ast::Expr) {
self.warn_if_doc(expr.attrs.iter(), cx);
}
}

declare_lint! {
pub UNCONDITIONAL_RECURSION,
Warn,
Expand Down
1 change: 1 addition & 0 deletions src/librustc_lint/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
UnusedImportBraces,
AnonymousParameters,
IllegalFloatLiteralPattern,
UnusedDocComment,
);

add_early_builtin_with_new!(sess,
Expand Down
7 changes: 7 additions & 0 deletions src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,13 @@ impl Stmt {
};
self
}

pub fn is_item(&self) -> bool {
match self.node {
StmtKind::Local(_) => true,
_ => false,
}
}
}

impl fmt::Debug for Stmt {
Expand Down
32 changes: 0 additions & 32 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2420,12 +2420,6 @@ impl<'a> Parser<'a> {
expr.map(|mut expr| {
attrs.extend::<Vec<_>>(expr.attrs.into());
expr.attrs = attrs;
if if let Some(ref doc) = expr.attrs.iter().find(|x| x.is_sugared_doc) {
self.span_fatal_err(doc.span, Error::UselessDocComment).emit();
true
} else { false } {
return expr;
}
match expr.node {
ExprKind::If(..) | ExprKind::IfLet(..) => {
if !expr.attrs.is_empty() {
Expand Down Expand Up @@ -3110,9 +3104,6 @@ impl<'a> Parser<'a> {

// `else` token already eaten
pub fn parse_else_expr(&mut self) -> PResult<'a, P<Expr>> {
if self.prev_token_kind == PrevTokenKind::DocComment {
return Err(self.span_fatal_err(self.span, Error::UselessDocComment));
}
if self.eat_keyword(keywords::If) {
return self.parse_if_expr(ThinVec::new());
} else {
Expand All @@ -3126,9 +3117,6 @@ impl<'a> Parser<'a> {
span_lo: Span,
mut attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>> {
// Parse: `for <src_pat> in <src_expr> <src_loop_block>`
if let Some(doc) = attrs.iter().find(|x| x.is_sugared_doc) {
self.span_fatal_err(doc.span, Error::UselessDocComment).emit();
}

let pat = self.parse_pat()?;
self.expect_keyword(keywords::In)?;
Expand All @@ -3144,9 +3132,6 @@ impl<'a> Parser<'a> {
pub fn parse_while_expr(&mut self, opt_ident: Option<ast::SpannedIdent>,
span_lo: Span,
mut attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>> {
if let Some(doc) = attrs.iter().find(|x| x.is_sugared_doc) {
self.span_fatal_err(doc.span, Error::UselessDocComment).emit();
}
if self.token.is_keyword(keywords::Let) {
return self.parse_while_let_expr(opt_ident, span_lo, attrs);
}
Expand Down Expand Up @@ -3175,9 +3160,6 @@ impl<'a> Parser<'a> {
pub fn parse_loop_expr(&mut self, opt_ident: Option<ast::SpannedIdent>,
span_lo: Span,
mut attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>> {
if let Some(doc) = attrs.iter().find(|x| x.is_sugared_doc) {
self.span_fatal_err(doc.span, Error::UselessDocComment).emit();
}
let (iattrs, body) = self.parse_inner_attrs_and_block()?;
attrs.extend(iattrs);
let span = span_lo.to(body.span);
Expand All @@ -3188,19 +3170,13 @@ impl<'a> Parser<'a> {
pub fn parse_catch_expr(&mut self, span_lo: Span, mut attrs: ThinVec<Attribute>)
-> PResult<'a, P<Expr>>
{
if let Some(doc) = attrs.iter().find(|x| x.is_sugared_doc) {
self.span_fatal_err(doc.span, Error::UselessDocComment).emit();
}
let (iattrs, body) = self.parse_inner_attrs_and_block()?;
attrs.extend(iattrs);
Ok(self.mk_expr(span_lo.to(body.span), ExprKind::Catch(body), attrs))
}

// `match` token already eaten
fn parse_match_expr(&mut self, mut attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>> {
if let Some(doc) = attrs.iter().find(|x| x.is_sugared_doc) {
self.span_fatal_err(doc.span, Error::UselessDocComment).emit();
}
let match_span = self.prev_span;
let lo = self.prev_span;
let discriminant = self.parse_expr_res(RESTRICTION_NO_STRUCT_LITERAL,
Expand Down Expand Up @@ -3238,9 +3214,6 @@ impl<'a> Parser<'a> {
maybe_whole!(self, NtArm, |x| x);

let attrs = self.parse_outer_attributes()?;
if let Some(doc) = attrs.iter().find(|x| x.is_sugared_doc) {
self.span_fatal_err(doc.span, Error::UselessDocComment).emit();
}
let pats = self.parse_pats()?;
let guard = if self.eat_keyword(keywords::If) {
Some(self.parse_expr()?)
Expand Down Expand Up @@ -3695,9 +3668,6 @@ impl<'a> Parser<'a> {

/// Parse a local variable declaration
fn parse_local(&mut self, attrs: ThinVec<Attribute>) -> PResult<'a, P<Local>> {
if let Some(doc) = attrs.iter().find(|x| x.is_sugared_doc) {
self.span_fatal_err(doc.span, Error::UselessDocComment).emit();
}
let lo = self.span;
let pat = self.parse_pat()?;

Expand Down Expand Up @@ -4187,8 +4157,6 @@ impl<'a> Parser<'a> {
stmts.push(stmt);
} else if self.token == token::Eof {
break;
} else if let token::DocComment(_) = self.token {
return Err(self.span_fatal_err(self.span, Error::UselessDocComment));
} else {
// Found only `;` or `}`.
continue;
Expand Down
28 changes: 16 additions & 12 deletions src/test/compile-fail/useless_comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,23 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

fn foo3() -> i32 {
let mut x = 12;
/// z //~ ERROR E0585
while x < 1 {
/// x //~ ERROR E0585
//~^ ERROR attributes on non-item statements and expressions are experimental
x += 1;
#![deny(unused_doc_comment)]

fn foo() {
/// a //~ ERROR unused doc comment
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this test needs updating now

let x = 12;

/// b //~ ERROR unused doc comment
match x {
/// c //~ ERROR unused doc comment
1 => {},
_ => {}
}
/// d //~ ERROR E0585
return x;

/// foo //~ ERROR unused doc comment
unsafe {}
}

fn main() {
/// e //~ ERROR E0585
foo3();
}
foo();
}
25 changes: 0 additions & 25 deletions src/test/compile-fail/useless_comment2.rs

This file was deleted.

22 changes: 0 additions & 22 deletions src/test/compile-fail/useless_comment3.rs

This file was deleted.