1 // This file incorporates work covered by the following copyright and
3 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
4 // file at the top-level directory of this distribution and at
5 // http://rust-lang.org/COPYRIGHT.
7 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
8 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
9 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
10 // option. This file may not be copied, modified, or distributed
11 // except according to those terms.
14 // Note: More specifically this lint is largely inspired (aka copied) from
20 // com/rust-lang/rust/blob/d6d05904697d89099b55da3331155392f1db9c00/src/librustc_lint/builtin.
32 use syntax::codemap::Span;
35 /// **What it does:** Warns if there is missing doc for any documentable item
36 /// (public or private).
38 /// **Why is this bad?** Doc is good. *rustc* has a `MISSING_DOCS`
39 /// allowed-by-default lint for
40 /// public members, but has no way to enforce documentation of private items.
41 /// This lint fixes that.
43 /// **Known problems:** None.
45 pub MISSING_DOCS_IN_PRIVATE_ITEMS,
47 "detects missing documentation for public and private members"
50 pub struct MissingDoc {
51 /// Stack of whether #[doc(hidden)] is set
52 /// at each level which has lint attributes.
53 doc_hidden_stack: Vec<bool>,
56 impl ::std::default::Default for MissingDoc {
57 fn default() -> MissingDoc {
63 pub fn new() -> MissingDoc {
64 MissingDoc { doc_hidden_stack: vec![false] }
67 fn doc_hidden(&self) -> bool {
68 *self.doc_hidden_stack.last().expect(
69 "empty doc_hidden_stack",
73 fn check_missing_docs_attrs(&self, cx: &LateContext, attrs: &[ast::Attribute], sp: Span, desc: &'static str) {
74 // If we're building a test harness, then warning about
75 // documentation is probably not really relevant right now.
76 if cx.sess().opts.test {
80 // `#[doc(hidden)]` disables missing_docs check.
81 if self.doc_hidden() {
89 let has_doc = attrs.iter().any(|a| {
90 a.is_value_str() && a.name().map_or(false, |n| n == "doc")
94 MISSING_DOCS_IN_PRIVATE_ITEMS,
96 &format!("missing documentation for {}", desc),
102 impl LintPass for MissingDoc {
103 fn get_lints(&self) -> LintArray {
104 lint_array![MISSING_DOCS_IN_PRIVATE_ITEMS]
108 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDoc {
109 fn enter_lint_attrs(&mut self, _: &LateContext<'a, 'tcx>, attrs: &'tcx [ast::Attribute]) {
110 let doc_hidden = self.doc_hidden() ||
111 attrs.iter().any(|attr| {
112 attr.check_name("doc") &&
113 match attr.meta_item_list() {
115 Some(l) => attr::list_contains_name(&l[..], "hidden"),
118 self.doc_hidden_stack.push(doc_hidden);
121 fn exit_lint_attrs(&mut self, _: &LateContext<'a, 'tcx>, _: &'tcx [ast::Attribute]) {
122 self.doc_hidden_stack.pop().expect("empty doc_hidden_stack");
125 fn check_crate(&mut self, cx: &LateContext<'a, 'tcx>, krate: &'tcx hir::Crate) {
126 self.check_missing_docs_attrs(cx, &krate.attrs, krate.span, "crate");
129 fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, it: &'tcx hir::Item) {
130 let desc = match it.node {
131 hir::ItemConst(..) => "a constant",
132 hir::ItemEnum(..) => "an enum",
133 hir::ItemFn(..) => "a function",
134 hir::ItemMod(..) => "a module",
135 hir::ItemStatic(..) => "a static",
136 hir::ItemStruct(..) => "a struct",
137 hir::ItemTrait(..) => "a trait",
138 hir::ItemGlobalAsm(..) => "an assembly blob",
139 hir::ItemTy(..) => "a type alias",
140 hir::ItemUnion(..) => "a union",
141 hir::ItemDefaultImpl(..) |
142 hir::ItemExternCrate(..) |
143 hir::ItemForeignMod(..) |
145 hir::ItemUse(..) => return,
148 self.check_missing_docs_attrs(cx, &it.attrs, it.span, desc);
151 fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, trait_item: &'tcx hir::TraitItem) {
152 let desc = match trait_item.node {
153 hir::TraitItemKind::Const(..) => "an associated constant",
154 hir::TraitItemKind::Method(..) => "a trait method",
155 hir::TraitItemKind::Type(..) => "an associated type",
158 self.check_missing_docs_attrs(cx, &trait_item.attrs, trait_item.span, desc);
161 fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, impl_item: &'tcx hir::ImplItem) {
162 // If the method is an impl for a trait, don't doc.
163 let def_id = cx.tcx.hir.local_def_id(impl_item.id);
164 match cx.tcx.associated_item(def_id).container {
165 ty::TraitContainer(_) => return,
166 ty::ImplContainer(cid) => {
167 if cx.tcx.impl_trait_ref(cid).is_some() {
173 let desc = match impl_item.node {
174 hir::ImplItemKind::Const(..) => "an associated constant",
175 hir::ImplItemKind::Method(..) => "a method",
176 hir::ImplItemKind::Type(_) => "an associated type",
178 self.check_missing_docs_attrs(cx, &impl_item.attrs, impl_item.span, desc);
181 fn check_struct_field(&mut self, cx: &LateContext<'a, 'tcx>, sf: &'tcx hir::StructField) {
182 if !sf.is_positional() {
183 self.check_missing_docs_attrs(cx, &sf.attrs, sf.span, "a struct field");
187 fn check_variant(&mut self, cx: &LateContext<'a, 'tcx>, v: &'tcx hir::Variant, _: &hir::Generics) {
188 self.check_missing_docs_attrs(cx, &v.node.attrs, v.span, "a variant");