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