]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/missing_doc.rs
Run rustfmt
[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
15 // *rustc*'s
16 // [`missing_doc`].
17 //
18 // [`missing_doc`]:
19 // https://github.
20 // com/rust-lang/rust/blob/d6d05904697d89099b55da3331155392f1db9c00/src/librustc_lint/builtin.
21 // 
22 //
23 //
24 //
25 //
26 //
27 // rs#L246
28 //
29
30 use rustc::hir;
31 use rustc::lint::*;
32 use rustc::ty;
33 use syntax::ast;
34 use syntax::attr;
35 use syntax::codemap::Span;
36 use utils::in_macro;
37
38 /// **What it does:** Warns if there is missing doc for any documentable item
39 /// (public or private).
40 ///
41 /// **Why is this bad?** Doc is good. *rustc* has a `MISSING_DOCS`
42 /// allowed-by-default lint for
43 /// public members, but has no way to enforce documentation of private items.
44 /// This lint fixes that.
45 ///
46 /// **Known problems:** None.
47 declare_lint! {
48     pub MISSING_DOCS_IN_PRIVATE_ITEMS,
49     Allow,
50     "detects missing documentation for public and private members"
51 }
52
53 pub struct MissingDoc {
54     /// Stack of whether #[doc(hidden)] is set
55     /// at each level which has lint attributes.
56     doc_hidden_stack: Vec<bool>,
57 }
58
59 impl ::std::default::Default for MissingDoc {
60     fn default() -> Self {
61         Self::new()
62     }
63 }
64
65 impl MissingDoc {
66     pub fn new() -> Self {
67         Self { doc_hidden_stack: vec![false] }
68     }
69
70     fn doc_hidden(&self) -> bool {
71         *self.doc_hidden_stack.last().expect(
72             "empty doc_hidden_stack",
73         )
74     }
75
76     fn check_missing_docs_attrs(&self, cx: &LateContext, attrs: &[ast::Attribute], sp: Span, desc: &'static str) {
77         // If we're building a test harness, then warning about
78         // documentation is probably not really relevant right now.
79         if cx.sess().opts.test {
80             return;
81         }
82
83         // `#[doc(hidden)]` disables missing_docs check.
84         if self.doc_hidden() {
85             return;
86         }
87
88         if in_macro(sp) {
89             return;
90         }
91
92         let has_doc = attrs.iter().any(|a| {
93             a.is_value_str() && a.name().map_or(false, |n| n == "doc")
94         });
95         if !has_doc {
96             cx.span_lint(
97                 MISSING_DOCS_IN_PRIVATE_ITEMS,
98                 sp,
99                 &format!("missing documentation for {}", desc),
100             );
101         }
102     }
103 }
104
105 impl LintPass for MissingDoc {
106     fn get_lints(&self) -> LintArray {
107         lint_array![MISSING_DOCS_IN_PRIVATE_ITEMS]
108     }
109 }
110
111 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDoc {
112     fn enter_lint_attrs(&mut self, _: &LateContext<'a, 'tcx>, attrs: &'tcx [ast::Attribute]) {
113         let doc_hidden = self.doc_hidden() ||
114             attrs.iter().any(|attr| {
115                 attr.check_name("doc") &&
116                     match attr.meta_item_list() {
117                         None => false,
118                         Some(l) => attr::list_contains_name(&l[..], "hidden"),
119                     }
120             });
121         self.doc_hidden_stack.push(doc_hidden);
122     }
123
124     fn exit_lint_attrs(&mut self, _: &LateContext<'a, 'tcx>, _: &'tcx [ast::Attribute]) {
125         self.doc_hidden_stack.pop().expect("empty doc_hidden_stack");
126     }
127
128     fn check_crate(&mut self, cx: &LateContext<'a, 'tcx>, krate: &'tcx hir::Crate) {
129         self.check_missing_docs_attrs(cx, &krate.attrs, krate.span, "crate");
130     }
131
132     fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, it: &'tcx hir::Item) {
133         let desc = match it.node {
134             hir::ItemConst(..) => "a constant",
135             hir::ItemEnum(..) => "an enum",
136             hir::ItemFn(..) => "a function",
137             hir::ItemMod(..) => "a module",
138             hir::ItemStatic(..) => "a static",
139             hir::ItemStruct(..) => "a struct",
140             hir::ItemTrait(..) => "a trait",
141             hir::ItemGlobalAsm(..) => "an assembly blob",
142             hir::ItemTy(..) => "a type alias",
143             hir::ItemUnion(..) => "a union",
144             hir::ItemDefaultImpl(..) |
145             hir::ItemExternCrate(..) |
146             hir::ItemForeignMod(..) |
147             hir::ItemImpl(..) |
148             hir::ItemUse(..) => return,
149         };
150
151         self.check_missing_docs_attrs(cx, &it.attrs, it.span, desc);
152     }
153
154     fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, trait_item: &'tcx hir::TraitItem) {
155         let desc = match trait_item.node {
156             hir::TraitItemKind::Const(..) => "an associated constant",
157             hir::TraitItemKind::Method(..) => "a trait method",
158             hir::TraitItemKind::Type(..) => "an associated type",
159         };
160
161         self.check_missing_docs_attrs(cx, &trait_item.attrs, trait_item.span, desc);
162     }
163
164     fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, impl_item: &'tcx hir::ImplItem) {
165         // If the method is an impl for a trait, don't doc.
166         let def_id = cx.tcx.hir.local_def_id(impl_item.id);
167         match cx.tcx.associated_item(def_id).container {
168             ty::TraitContainer(_) => return,
169             ty::ImplContainer(cid) => {
170                 if cx.tcx.impl_trait_ref(cid).is_some() {
171                     return;
172                 }
173             },
174         }
175
176         let desc = match impl_item.node {
177             hir::ImplItemKind::Const(..) => "an associated constant",
178             hir::ImplItemKind::Method(..) => "a method",
179             hir::ImplItemKind::Type(_) => "an associated type",
180         };
181         self.check_missing_docs_attrs(cx, &impl_item.attrs, impl_item.span, desc);
182     }
183
184     fn check_struct_field(&mut self, cx: &LateContext<'a, 'tcx>, sf: &'tcx hir::StructField) {
185         if !sf.is_positional() {
186             self.check_missing_docs_attrs(cx, &sf.attrs, sf.span, "a struct field");
187         }
188     }
189
190     fn check_variant(&mut self, cx: &LateContext<'a, 'tcx>, v: &'tcx hir::Variant, _: &hir::Generics) {
191         self.check_missing_docs_attrs(cx, &v.node.attrs, v.span, "a variant");
192     }
193 }