]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/missing_doc.rs
70696ea3908beb1dae77522446eaeeb8d03aa558
[rust.git] / clippy_lints / src / missing_doc.rs
1 // This file incorporates work covered by the following copyright and
2 // permission notice:
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.
6 //
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.
12 //
13
14 // Note: More specifically this lint is largely inspired (aka copied) from *rustc*'s
15 // [`missing_doc`].
16 //
17 // [`missing_doc`]:
18 // https://github.
19 // com/rust-lang/rust/blob/d6d05904697d89099b55da3331155392f1db9c00/src/librustc_lint/builtin.
20 // rs#L246
21 //
22
23 use rustc::hir;
24 use rustc::lint::*;
25 use rustc::ty;
26 use syntax::ast;
27 use syntax::attr;
28 use syntax::codemap::Span;
29 use utils::in_macro;
30
31 /// **What it does:** Warns if there is missing doc for any documentable item (public or private).
32 ///
33 /// **Why is this bad?** Doc is good. *rustc* has a `MISSING_DOCS` allowed-by-default lint for
34 /// public members, but has no way to enforce documentation of private items. This lint fixes that.
35 ///
36 /// **Known problems:** None.
37 declare_lint! {
38     pub MISSING_DOCS_IN_PRIVATE_ITEMS,
39     Allow,
40     "detects missing documentation for public and private members"
41 }
42
43 pub struct MissingDoc {
44     /// Stack of whether #[doc(hidden)] is set
45     /// at each level which has lint attributes.
46     doc_hidden_stack: Vec<bool>,
47 }
48
49 impl ::std::default::Default for MissingDoc {
50     fn default() -> MissingDoc {
51         MissingDoc::new()
52     }
53 }
54
55 impl MissingDoc {
56     pub fn new() -> MissingDoc {
57         MissingDoc { doc_hidden_stack: vec![false] }
58     }
59
60     fn doc_hidden(&self) -> bool {
61         *self.doc_hidden_stack.last().expect("empty doc_hidden_stack")
62     }
63
64     fn check_missing_docs_attrs(&self, cx: &LateContext, attrs: &[ast::Attribute], sp: Span, desc: &'static str) {
65         // If we're building a test harness, then warning about
66         // documentation is probably not really relevant right now.
67         if cx.sess().opts.test {
68             return;
69         }
70
71         // `#[doc(hidden)]` disables missing_docs check.
72         if self.doc_hidden() {
73             return;
74         }
75
76         if in_macro(cx, sp) {
77             return;
78         }
79
80         let has_doc = attrs.iter().any(|a| a.is_value_str() && a.name() == "doc");
81         if !has_doc {
82             cx.span_lint(MISSING_DOCS_IN_PRIVATE_ITEMS,
83                          sp,
84                          &format!("missing documentation for {}", desc));
85         }
86     }
87 }
88
89 impl LintPass for MissingDoc {
90     fn get_lints(&self) -> LintArray {
91         lint_array![MISSING_DOCS_IN_PRIVATE_ITEMS]
92     }
93 }
94
95 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDoc {
96     fn enter_lint_attrs(&mut self, _: &LateContext<'a, 'tcx>, attrs: &'tcx [ast::Attribute]) {
97         let doc_hidden = self.doc_hidden() ||
98                          attrs.iter().any(|attr| {
99             attr.check_name("doc") &&
100             match attr.meta_item_list() {
101                 None => false,
102                 Some(l) => attr::list_contains_name(&l[..], "hidden"),
103             }
104         });
105         self.doc_hidden_stack.push(doc_hidden);
106     }
107
108     fn exit_lint_attrs(&mut self, _: &LateContext<'a, 'tcx>, _: &'tcx [ast::Attribute]) {
109         self.doc_hidden_stack.pop().expect("empty doc_hidden_stack");
110     }
111
112     fn check_crate(&mut self, cx: &LateContext<'a, 'tcx>, krate: &'tcx hir::Crate) {
113         self.check_missing_docs_attrs(cx, &krate.attrs, krate.span, "crate");
114     }
115
116     fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, it: &'tcx 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(..) |
130             hir::ItemImpl(..) |
131             hir::ItemUse(..) => return,
132         };
133
134         self.check_missing_docs_attrs(cx, &it.attrs, it.span, desc);
135     }
136
137     fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, trait_item: &'tcx 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",
142         };
143
144         self.check_missing_docs_attrs(cx, &trait_item.attrs, trait_item.span, desc);
145     }
146
147     fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, impl_item: &'tcx 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() {
154                     return;
155                 }
156             },
157         }
158
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",
163         };
164         self.check_missing_docs_attrs(cx, &impl_item.attrs, impl_item.span, desc);
165     }
166
167     fn check_struct_field(&mut self, cx: &LateContext<'a, 'tcx>, sf: &'tcx hir::StructField) {
168         if !sf.is_positional() {
169             self.check_missing_docs_attrs(cx, &sf.attrs, sf.span, "a struct field");
170         }
171     }
172
173     fn check_variant(&mut self, cx: &LateContext<'a, 'tcx>, v: &'tcx hir::Variant, _: &hir::Generics) {
174         self.check_missing_docs_attrs(cx, &v.node.attrs, v.span, "a variant");
175     }
176 }