Skip to content

Commit 53bf790

Browse files
committed
Auto merge of #43009 - GuillaumeGomez:unused-doc-comments, r=nrc
Throw errors when doc comments are added where they're unused #42617
2 parents cfe1668 + 3142ca0 commit 53bf790

File tree

11 files changed

+112
-37
lines changed

11 files changed

+112
-37
lines changed

src/Cargo.lock

+11-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/librustc/hir/mod.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,13 @@ impl Decl_ {
892892
DeclItem(_) => &[]
893893
}
894894
}
895+
896+
pub fn is_local(&self) -> bool {
897+
match *self {
898+
Decl_::DeclLocal(_) => true,
899+
_ => false,
900+
}
901+
}
895902
}
896903

897904
/// represents one arm of a 'match'
@@ -1679,7 +1686,7 @@ pub struct Item {
16791686

16801687
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
16811688
pub enum Item_ {
1682-
/// An`extern crate` item, with optional original crate name,
1689+
/// An `extern crate` item, with optional original crate name,
16831690
///
16841691
/// e.g. `extern crate foo` or `extern crate foo_bar as foo`
16851692
ItemExternCrate(Option<Name>),

src/librustc/middle/region.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -458,10 +458,10 @@ impl<'tcx> RegionMaps {
458458
-> CodeExtent {
459459
if scope_a == scope_b { return scope_a; }
460460

461-
/// [1] The initial values for `a_buf` and `b_buf` are not used.
462-
/// The `ancestors_of` function will return some prefix that
463-
/// is re-initialized with new values (or else fallback to a
464-
/// heap-allocated vector).
461+
// [1] The initial values for `a_buf` and `b_buf` are not used.
462+
// The `ancestors_of` function will return some prefix that
463+
// is re-initialized with new values (or else fallback to a
464+
// heap-allocated vector).
465465
let mut a_buf: [CodeExtent; 32] = [scope_a /* [1] */; 32];
466466
let mut a_vec: Vec<CodeExtent> = vec![];
467467
let mut b_buf: [CodeExtent; 32] = [scope_b /* [1] */; 32];

src/librustc_lint/builtin.rs

+40
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,46 @@ impl EarlyLintPass for IllegalFloatLiteralPattern {
722722
}
723723
}
724724

725+
declare_lint! {
726+
pub UNUSED_DOC_COMMENT,
727+
Warn,
728+
"detects doc comments that aren't used by rustdoc"
729+
}
730+
731+
#[derive(Copy, Clone)]
732+
pub struct UnusedDocComment;
733+
734+
impl LintPass for UnusedDocComment {
735+
fn get_lints(&self) -> LintArray {
736+
lint_array![UNUSED_DOC_COMMENT]
737+
}
738+
}
739+
740+
impl UnusedDocComment {
741+
fn warn_if_doc<'a, 'tcx,
742+
I: Iterator<Item=&'a ast::Attribute>,
743+
C: LintContext<'tcx>>(&self, mut attrs: I, cx: &C) {
744+
if let Some(attr) = attrs.find(|a| a.is_value_str() && a.check_name("doc")) {
745+
cx.struct_span_lint(UNUSED_DOC_COMMENT, attr.span, "doc comment not used by rustdoc")
746+
.emit();
747+
}
748+
}
749+
}
750+
751+
impl EarlyLintPass for UnusedDocComment {
752+
fn check_local(&mut self, cx: &EarlyContext, decl: &ast::Local) {
753+
self.warn_if_doc(decl.attrs.iter(), cx);
754+
}
755+
756+
fn check_arm(&mut self, cx: &EarlyContext, arm: &ast::Arm) {
757+
self.warn_if_doc(arm.attrs.iter(), cx);
758+
}
759+
760+
fn check_expr(&mut self, cx: &EarlyContext, expr: &ast::Expr) {
761+
self.warn_if_doc(expr.attrs.iter(), cx);
762+
}
763+
}
764+
725765
declare_lint! {
726766
pub UNCONDITIONAL_RECURSION,
727767
Warn,

src/librustc_lint/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
111111
UnusedImportBraces,
112112
AnonymousParameters,
113113
IllegalFloatLiteralPattern,
114+
UnusedDocComment,
114115
);
115116

116117
add_early_builtin_with_new!(sess,

src/librustc_typeck/check/wfcheck.rs

+17-17
Original file line numberDiff line numberDiff line change
@@ -89,23 +89,23 @@ impl<'a, 'gcx> CheckTypeWellFormedVisitor<'a, 'gcx> {
8989
tcx.item_path_str(tcx.hir.local_def_id(item.id)));
9090

9191
match item.node {
92-
/// Right now we check that every default trait implementation
93-
/// has an implementation of itself. Basically, a case like:
94-
///
95-
/// `impl Trait for T {}`
96-
///
97-
/// has a requirement of `T: Trait` which was required for default
98-
/// method implementations. Although this could be improved now that
99-
/// there's a better infrastructure in place for this, it's being left
100-
/// for a follow-up work.
101-
///
102-
/// Since there's such a requirement, we need to check *just* positive
103-
/// implementations, otherwise things like:
104-
///
105-
/// impl !Send for T {}
106-
///
107-
/// won't be allowed unless there's an *explicit* implementation of `Send`
108-
/// for `T`
92+
// Right now we check that every default trait implementation
93+
// has an implementation of itself. Basically, a case like:
94+
//
95+
// `impl Trait for T {}`
96+
//
97+
// has a requirement of `T: Trait` which was required for default
98+
// method implementations. Although this could be improved now that
99+
// there's a better infrastructure in place for this, it's being left
100+
// for a follow-up work.
101+
//
102+
// Since there's such a requirement, we need to check *just* positive
103+
// implementations, otherwise things like:
104+
//
105+
// impl !Send for T {}
106+
//
107+
// won't be allowed unless there's an *explicit* implementation of `Send`
108+
// for `T`
109109
hir::ItemImpl(_, hir::ImplPolarity::Positive, _, _,
110110
ref trait_ref, ref self_ty, _) => {
111111
self.check_impl(item, self_ty, trait_ref);

src/libsyntax/ast.rs

+7
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,13 @@ impl Stmt {
733733
};
734734
self
735735
}
736+
737+
pub fn is_item(&self) -> bool {
738+
match self.node {
739+
StmtKind::Local(_) => true,
740+
_ => false,
741+
}
742+
}
736743
}
737744

738745
impl fmt::Debug for Stmt {

src/libsyntax/parse/parser.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -2074,14 +2074,14 @@ impl<'a> Parser<'a> {
20742074
} else {
20752075
Ok(self.mk_expr(span, ExprKind::Tup(es), attrs))
20762076
}
2077-
},
2077+
}
20782078
token::OpenDelim(token::Brace) => {
20792079
return self.parse_block_expr(lo, BlockCheckMode::Default, attrs);
2080-
},
2081-
token::BinOp(token::Or) | token::OrOr => {
2080+
}
2081+
token::BinOp(token::Or) | token::OrOr => {
20822082
let lo = self.span;
20832083
return self.parse_lambda_expr(lo, CaptureBy::Ref, attrs);
2084-
},
2084+
}
20852085
token::OpenDelim(token::Bracket) => {
20862086
self.bump();
20872087

@@ -2329,7 +2329,6 @@ impl<'a> Parser<'a> {
23292329
pub fn parse_block_expr(&mut self, lo: Span, blk_mode: BlockCheckMode,
23302330
outer_attrs: ThinVec<Attribute>)
23312331
-> PResult<'a, P<Expr>> {
2332-
23332332
self.expect(&token::OpenDelim(token::Brace))?;
23342333

23352334
let mut attrs = outer_attrs;

src/test/compile-fail/issue-34222.rs renamed to src/test/compile-fail/useless_comment.rs

+18-6
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,23 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(rustc_attrs)]
12-
#![allow(warnings)]
11+
#![deny(unused_doc_comment)]
1312

14-
#[rustc_error]
15-
fn main() { //~ ERROR compilation successful
16-
/// crash
17-
let x = 0;
13+
fn foo() {
14+
/// a //~ ERROR doc comment not used by rustdoc
15+
let x = 12;
16+
17+
/// b //~ doc comment not used by rustdoc
18+
match x {
19+
/// c //~ ERROR doc comment not used by rustdoc
20+
1 => {},
21+
_ => {}
22+
}
23+
24+
/// foo //~ ERROR doc comment not used by rustdoc
25+
unsafe {}
1826
}
27+
28+
fn main() {
29+
foo();
30+
}

src/tools/rls

Submodule rls updated from 79d659e to 06b48d1

0 commit comments

Comments
 (0)