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