Skip to content

Commit 30a5612

Browse files
committed
Check crate root for docs in missing_doc lint.
Because the root module isn't actually an item, we need to do some hackish handling of it. Closes #10656.
1 parent ad58e2c commit 30a5612

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

Diff for: src/librustc/middle/lint.rs

+16-10
Original file line numberDiff line numberDiff line change
@@ -1048,7 +1048,7 @@ fn check_unnecessary_allocation(cx: &Context, e: &ast::Expr) {
10481048
}
10491049

10501050
fn check_missing_doc_attrs(cx: &Context,
1051-
id: ast::NodeId,
1051+
id: Option<ast::NodeId>,
10521052
attrs: &[ast::Attribute],
10531053
sp: Span,
10541054
desc: &'static str) {
@@ -1059,17 +1059,20 @@ fn check_missing_doc_attrs(cx: &Context,
10591059
// `#[doc(hidden)]` disables missing_doc check.
10601060
if cx.is_doc_hidden { return }
10611061

1062-
// Only check publicly-visible items, using the result from the
1063-
// privacy pass.
1064-
if !cx.exported_items.contains(&id) { return }
1062+
// Only check publicly-visible items, using the result from the privacy pass. It's an option so
1063+
// the crate root can also use this function (it doesn't have a NodeId).
1064+
match id {
1065+
Some(ref id) if !cx.exported_items.contains(id) => return,
1066+
_ => ()
1067+
}
10651068

10661069
if !attrs.iter().any(|a| a.node.is_sugared_doc) {
10671070
cx.span_lint(missing_doc, sp,
10681071
format!("missing documentation for {}", desc));
10691072
}
10701073
}
10711074

1072-
fn check_missing_doc_item(cx: &mut Context, it: &ast::item) { // XXX doesn't need to be mut
1075+
fn check_missing_doc_item(cx: &Context, it: &ast::item) {
10731076
let desc = match it.node {
10741077
ast::item_fn(..) => "a function",
10751078
ast::item_mod(..) => "a module",
@@ -1078,7 +1081,7 @@ fn check_missing_doc_item(cx: &mut Context, it: &ast::item) { // XXX doesn't nee
10781081
ast::item_trait(..) => "a trait",
10791082
_ => return
10801083
};
1081-
check_missing_doc_attrs(cx, it.id, it.attrs, it.span, desc);
1084+
check_missing_doc_attrs(cx, Some(it.id), it.attrs, it.span, desc);
10821085
}
10831086

10841087
fn check_missing_doc_method(cx: &Context, m: &ast::method) {
@@ -1104,24 +1107,24 @@ fn check_missing_doc_method(cx: &Context, m: &ast::method) {
11041107
}
11051108
}
11061109
}
1107-
check_missing_doc_attrs(cx, m.id, m.attrs, m.span, "a method");
1110+
check_missing_doc_attrs(cx, Some(m.id), m.attrs, m.span, "a method");
11081111
}
11091112

11101113
fn check_missing_doc_ty_method(cx: &Context, tm: &ast::TypeMethod) {
1111-
check_missing_doc_attrs(cx, tm.id, tm.attrs, tm.span, "a type method");
1114+
check_missing_doc_attrs(cx, Some(tm.id), tm.attrs, tm.span, "a type method");
11121115
}
11131116

11141117
fn check_missing_doc_struct_field(cx: &Context, sf: &ast::struct_field) {
11151118
match sf.node.kind {
11161119
ast::named_field(_, vis) if vis != ast::private =>
1117-
check_missing_doc_attrs(cx, cx.cur_struct_def_id, sf.node.attrs,
1120+
check_missing_doc_attrs(cx, Some(cx.cur_struct_def_id), sf.node.attrs,
11181121
sf.span, "a struct field"),
11191122
_ => {}
11201123
}
11211124
}
11221125

11231126
fn check_missing_doc_variant(cx: &Context, v: &ast::variant) {
1124-
check_missing_doc_attrs(cx, v.node.id, v.node.attrs, v.span, "a variant");
1127+
check_missing_doc_attrs(cx, Some(v.node.id), v.node.attrs, v.span, "a variant");
11251128
}
11261129

11271130
/// Checks for use of items with #[deprecated], #[experimental] and
@@ -1372,6 +1375,9 @@ pub fn check_crate(tcx: ty::ctxt,
13721375
});
13731376

13741377
check_crate_attrs_usage(cx, crate.attrs);
1378+
// since the root module isn't visited as an item (because it isn't an item), warn for it
1379+
// here.
1380+
check_missing_doc_attrs(cx, None, crate.attrs, crate.span, "crate");
13751381

13761382
visit::walk_crate(cx, crate, ());
13771383
});

Diff for: src/libsyntax/visit.rs

+1
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ pub fn walk_mod<E:Clone, V:Visitor<E>>(visitor: &mut V, module: &_mod, env: E) {
130130
for view_item in module.view_items.iter() {
131131
visitor.visit_view_item(view_item, env.clone())
132132
}
133+
133134
for item in module.items.iter() {
134135
visitor.visit_item(*item, env.clone())
135136
}

Diff for: src/test/compile-fail/issue-10656.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// error-pattern: missing documentation for crate
12+
13+
#[deny(missing_doc)];
14+
#[crate_type="lib"];

Diff for: src/test/compile-fail/lint-missing-doc.rs

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
#[feature(globs)];
1515
#[deny(missing_doc)];
1616

17+
//! Some garbage docs for the crate here
18+
#[doc="More garbage"];
19+
1720
struct Foo {
1821
a: int,
1922
priv b: int,

0 commit comments

Comments
 (0)