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 *rustc*'s
17 * [`missing_doc`]: https://github.com/rust-lang/rust/blob/d6d05904697d89099b55da3331155392f1db9c00/src/librustc_lint/builtin.rs#L246
25 use syntax::codemap::Span;
28 /// **What it does:** Warns if there is missing doc for any documentable item (public or private).
30 /// **Why is this bad?** Doc is good. *rustc* has a `MISSING_DOCS` allowed-by-default lint for
31 /// public members, but has no way to enforce documentation of private items. This lint fixes that.
33 /// **Known problems:** None.
35 pub MISSING_DOCS_IN_PRIVATE_ITEMS,
37 "detects missing documentation for public and private members"
40 pub struct MissingDoc {
41 /// Stack of whether #[doc(hidden)] is set
42 /// at each level which has lint attributes.
43 doc_hidden_stack: Vec<bool>,
46 impl ::std::default::Default for MissingDoc {
47 fn default() -> MissingDoc {
53 pub fn new() -> MissingDoc {
55 doc_hidden_stack: vec![false],
59 fn doc_hidden(&self) -> bool {
60 *self.doc_hidden_stack.last().expect("empty doc_hidden_stack")
63 fn check_missing_docs_attrs(&self,
65 attrs: &[ast::Attribute],
68 // If we're building a test harness, then warning about
69 // documentation is probably not really relevant right now.
70 if cx.sess().opts.test {
74 // `#[doc(hidden)]` disables missing_docs check.
75 if self.doc_hidden() {
83 let has_doc = attrs.iter().any(|a| a.is_value_str() && a.name() == "doc");
85 cx.span_lint(MISSING_DOCS_IN_PRIVATE_ITEMS, sp,
86 &format!("missing documentation for {}", desc));
91 impl LintPass for MissingDoc {
92 fn get_lints(&self) -> LintArray {
93 lint_array![MISSING_DOCS_IN_PRIVATE_ITEMS]
97 impl LateLintPass for MissingDoc {
98 fn enter_lint_attrs(&mut self, _: &LateContext, attrs: &[ast::Attribute]) {
99 let doc_hidden = self.doc_hidden() || attrs.iter().any(|attr| {
100 attr.check_name("doc") && match attr.meta_item_list() {
102 Some(l) => attr::list_contains_name(&l[..], "hidden"),
105 self.doc_hidden_stack.push(doc_hidden);
108 fn exit_lint_attrs(&mut self, _: &LateContext, _: &[ast::Attribute]) {
109 self.doc_hidden_stack.pop().expect("empty doc_hidden_stack");
112 fn check_crate(&mut self, cx: &LateContext, krate: &hir::Crate) {
113 self.check_missing_docs_attrs(cx, &krate.attrs, krate.span, "crate");
116 fn check_item(&mut self, cx: &LateContext, it: &hir::Item) {
117 let desc = match it.node {
118 hir::ItemConst(..) => "a constant",
119 hir::ItemEnum(..) => "an enum",
120 hir::ItemFn(..) => "a function",
121 hir::ItemMod(..) => "a module",
122 hir::ItemStatic(..) => "a static",
123 hir::ItemStruct(..) => "a struct",
124 hir::ItemTrait(..) => "a trait",
125 hir::ItemTy(..) => "a type alias",
126 hir::ItemUnion(..) => "a union",
127 hir::ItemDefaultImpl(..) |
128 hir::ItemExternCrate(..) |
129 hir::ItemForeignMod(..) |
131 hir::ItemUse(..) => return,
134 self.check_missing_docs_attrs(cx, &it.attrs, it.span, desc);
137 fn check_trait_item(&mut self, cx: &LateContext, trait_item: &hir::TraitItem) {
138 let desc = match trait_item.node {
139 hir::ConstTraitItem(..) => "an associated constant",
140 hir::MethodTraitItem(..) => "a trait method",
141 hir::TypeTraitItem(..) => "an associated type",
144 self.check_missing_docs_attrs(cx, &trait_item.attrs, trait_item.span, desc);
147 fn check_impl_item(&mut self, cx: &LateContext, impl_item: &hir::ImplItem) {
148 // If the method is an impl for a trait, don't doc.
149 let def_id = cx.tcx.map.local_def_id(impl_item.id);
150 match cx.tcx.associated_item(def_id).container {
151 ty::TraitContainer(_) => return,
152 ty::ImplContainer(cid) => {
153 if cx.tcx.impl_trait_ref(cid).is_some() {
159 let desc = match impl_item.node {
160 hir::ImplItemKind::Const(..) => "an associated constant",
161 hir::ImplItemKind::Method(..) => "a method",
162 hir::ImplItemKind::Type(_) => "an associated type",
164 self.check_missing_docs_attrs(cx, &impl_item.attrs, impl_item.span, desc);
167 fn check_struct_field(&mut self, cx: &LateContext, sf: &hir::StructField) {
168 if !sf.is_positional() {
169 self.check_missing_docs_attrs(cx, &sf.attrs, sf.span, "a struct field");
173 fn check_variant(&mut self, cx: &LateContext, v: &hir::Variant, _: &hir::Generics) {
174 self.check_missing_docs_attrs(cx, &v.node.attrs, v.span, "a variant");