]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/missing_doc.rs
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 // rs#L246
25 //
26
27 use rustc::hir;
28 use rustc::lint::*;
29 use rustc::ty;
30 use syntax::ast;
31 use syntax::attr;
32 use syntax::codemap::Span;
33 use utils::in_macro;
34
35 /// **What it does:** Warns if there is missing doc for any documentable item
36 /// (public or private).
37 ///
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.
42 ///
43 /// **Known problems:** None.
44 declare_lint! {
45     pub MISSING_DOCS_IN_PRIVATE_ITEMS,
46     Allow,
47     "detects missing documentation for public and private members"
48 }
49
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>,
54 }
55
56 impl ::std::default::Default for MissingDoc {
57     fn default() -> MissingDoc {
58         MissingDoc::new()
59     }
60 }
61
62 impl MissingDoc {
63     pub fn new() -> MissingDoc {
64         MissingDoc { doc_hidden_stack: vec![false] }
65     }
66
67     fn doc_hidden(&self) -> bool {
68         *self.doc_hidden_stack.last().expect(
69             "empty doc_hidden_stack",
70         )
71     }
72
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 {
77             return;
78         }
79
80         // `#[doc(hidden)]` disables missing_docs check.
81         if self.doc_hidden() {
82             return;
83         }
84
85         if in_macro(sp) {
86             return;
87         }
88
89         let has_doc = attrs.iter().any(|a| {
90             a.is_value_str() && a.name().map_or(false, |n| n == "doc")
91         });
92         if !has_doc {
93             cx.span_lint(
94                 MISSING_DOCS_IN_PRIVATE_ITEMS,
95                 sp,
96                 &format!("missing documentation for {}", desc),
97             );
98         }
99     }
100 }
101
102 impl LintPass for MissingDoc {
103     fn get_lints(&self) -> LintArray {
104         lint_array![MISSING_DOCS_IN_PRIVATE_ITEMS]
105     }
106 }
107
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() {
114                         None => false,
115                         Some(l) => attr::list_contains_name(&l[..], "hidden"),
116                     }
117             });
118         self.doc_hidden_stack.push(doc_hidden);
119     }
120
121     fn exit_lint_attrs(&mut self, _: &LateContext<'a, 'tcx>, _: &'tcx [ast::Attribute]) {
122         self.doc_hidden_stack.pop().expect("empty doc_hidden_stack");
123     }
124
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");
127     }
128
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(..) |
144             hir::ItemImpl(..) |
145             hir::ItemUse(..) => return,
146         };
147
148         self.check_missing_docs_attrs(cx, &it.attrs, it.span, desc);
149     }
150
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",
156         };
157
158         self.check_missing_docs_attrs(cx, &trait_item.attrs, trait_item.span, desc);
159     }
160
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() {
168                     return;
169                 }
170             },
171         }
172
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",
177         };
178         self.check_missing_docs_attrs(cx, &impl_item.attrs, impl_item.span, desc);
179     }
180
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");
184         }
185     }
186
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");
189     }
190 }