]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/missing_doc.rs
Merge pull request #3265 from mikerite/fix-export
[rust.git] / clippy_lints / src / missing_doc.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10
11 // This file incorporates work covered by the following copyright and
12 // permission notice:
13 //   Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
14 //   file at the top-level directory of this distribution and at
15 //   http://rust-lang.org/COPYRIGHT.
16 //
17 //   Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
18 //   http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
19 //   <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
20 //   option. This file may not be copied, modified, or distributed
21 //   except according to those terms.
22 //
23
24 // Note: More specifically this lint is largely inspired (aka copied) from
25 // *rustc*'s
26 // [`missing_doc`].
27 //
28 // [`missing_doc`]: https://github.com/rust-lang/rust/blob/d6d05904697d89099b55da3331155392f1db9c00/src/librustc_lint/builtin.rs#L246
29 //
30
31 use crate::rustc::hir;
32 use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass, LintContext};
33 use crate::rustc::{declare_tool_lint, lint_array};
34 use crate::rustc::ty;
35 use crate::syntax::ast;
36 use crate::syntax::attr;
37 use crate::syntax::source_map::Span;
38 use crate::utils::{span_lint, in_macro};
39
40 /// **What it does:** Warns if there is missing doc for any documentable item
41 /// (public or private).
42 ///
43 /// **Why is this bad?** Doc is good. *rustc* has a `MISSING_DOCS`
44 /// allowed-by-default lint for
45 /// public members, but has no way to enforce documentation of private items.
46 /// This lint fixes that.
47 ///
48 /// **Known problems:** None.
49 declare_clippy_lint! {
50     pub MISSING_DOCS_IN_PRIVATE_ITEMS,
51     restriction,
52     "detects missing documentation for public and private members"
53 }
54
55 pub struct MissingDoc {
56     /// Stack of whether #[doc(hidden)] is set
57     /// at each level which has lint attributes.
58     doc_hidden_stack: Vec<bool>,
59 }
60
61 impl ::std::default::Default for MissingDoc {
62     fn default() -> Self {
63         Self::new()
64     }
65 }
66
67 impl MissingDoc {
68     pub fn new() -> Self {
69         Self {
70             doc_hidden_stack: vec![false],
71         }
72     }
73
74     fn doc_hidden(&self) -> bool {
75         *self.doc_hidden_stack
76             .last()
77             .expect("empty doc_hidden_stack")
78     }
79
80     fn check_missing_docs_attrs(&self, cx: &LateContext<'_, '_>, attrs: &[ast::Attribute], sp: Span, desc: &'static str) {
81         // If we're building a test harness, then warning about
82         // documentation is probably not really relevant right now.
83         if cx.sess().opts.test {
84             return;
85         }
86
87         // `#[doc(hidden)]` disables missing_docs check.
88         if self.doc_hidden() {
89             return;
90         }
91
92         if in_macro(sp) {
93             return;
94         }
95
96         let has_doc = attrs
97             .iter()
98             .any(|a| a.is_value_str() && a.name() == "doc");
99         if !has_doc {
100             span_lint(
101                 cx,
102                 MISSING_DOCS_IN_PRIVATE_ITEMS,
103                 sp,
104                 &format!("missing documentation for {}", desc),
105             );
106         }
107     }
108 }
109
110 impl LintPass for MissingDoc {
111     fn get_lints(&self) -> LintArray {
112         lint_array![MISSING_DOCS_IN_PRIVATE_ITEMS]
113     }
114 }
115
116 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDoc {
117     fn enter_lint_attrs(&mut self, _: &LateContext<'a, 'tcx>, attrs: &'tcx [ast::Attribute]) {
118         let doc_hidden = self.doc_hidden() || attrs.iter().any(|attr| {
119             attr.check_name("doc") && match attr.meta_item_list() {
120                 None => false,
121                 Some(l) => attr::list_contains_name(&l[..], "hidden"),
122             }
123         });
124         self.doc_hidden_stack.push(doc_hidden);
125     }
126
127     fn exit_lint_attrs(&mut self, _: &LateContext<'a, 'tcx>, _: &'tcx [ast::Attribute]) {
128         self.doc_hidden_stack.pop().expect("empty doc_hidden_stack");
129     }
130
131     fn check_crate(&mut self, cx: &LateContext<'a, 'tcx>, krate: &'tcx hir::Crate) {
132         self.check_missing_docs_attrs(cx, &krate.attrs, krate.span, "crate");
133     }
134
135     fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, it: &'tcx hir::Item) {
136         let desc = match it.node {
137             hir::ItemKind::Const(..) => "a constant",
138             hir::ItemKind::Enum(..) => "an enum",
139             hir::ItemKind::Fn(..) => {
140                 // ignore main()
141                 if it.name == "main" {
142                     let def_id = cx.tcx.hir.local_def_id(it.id);
143                     let def_key = cx.tcx.hir.def_key(def_id);
144                     if def_key.parent == Some(hir::def_id::CRATE_DEF_INDEX) {
145                         return;
146                     }
147                 }
148                 "a function"
149             },
150             hir::ItemKind::Mod(..) => "a module",
151             hir::ItemKind::Static(..) => "a static",
152             hir::ItemKind::Struct(..) => "a struct",
153             hir::ItemKind::Trait(..) => "a trait",
154             hir::ItemKind::TraitAlias(..) => "a trait alias",
155             hir::ItemKind::GlobalAsm(..) => "an assembly blob",
156             hir::ItemKind::Ty(..) => "a type alias",
157             hir::ItemKind::Union(..) => "a union",
158             hir::ItemKind::Existential(..) => "an existential type",
159             hir::ItemKind::ExternCrate(..) |
160             hir::ItemKind::ForeignMod(..) |
161             hir::ItemKind::Impl(..) |
162             hir::ItemKind::Use(..) => return,
163         };
164
165         self.check_missing_docs_attrs(cx, &it.attrs, it.span, desc);
166     }
167
168     fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, trait_item: &'tcx hir::TraitItem) {
169         let desc = match trait_item.node {
170             hir::TraitItemKind::Const(..) => "an associated constant",
171             hir::TraitItemKind::Method(..) => "a trait method",
172             hir::TraitItemKind::Type(..) => "an associated type",
173         };
174
175         self.check_missing_docs_attrs(cx, &trait_item.attrs, trait_item.span, desc);
176     }
177
178     fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, impl_item: &'tcx hir::ImplItem) {
179         // If the method is an impl for a trait, don't doc.
180         let def_id = cx.tcx.hir.local_def_id(impl_item.id);
181         match cx.tcx.associated_item(def_id).container {
182             ty::TraitContainer(_) => return,
183             ty::ImplContainer(cid) => if cx.tcx.impl_trait_ref(cid).is_some() {
184                 return;
185             },
186         }
187
188         let desc = match impl_item.node {
189             hir::ImplItemKind::Const(..) => "an associated constant",
190             hir::ImplItemKind::Method(..) => "a method",
191             hir::ImplItemKind::Type(_) => "an associated type",
192             hir::ImplItemKind::Existential(_) => "an existential type",
193         };
194         self.check_missing_docs_attrs(cx, &impl_item.attrs, impl_item.span, desc);
195     }
196
197     fn check_struct_field(&mut self, cx: &LateContext<'a, 'tcx>, sf: &'tcx hir::StructField) {
198         if !sf.is_positional() {
199             self.check_missing_docs_attrs(cx, &sf.attrs, sf.span, "a struct field");
200         }
201     }
202
203     fn check_variant(&mut self, cx: &LateContext<'a, 'tcx>, v: &'tcx hir::Variant, _: &hir::Generics) {
204         self.check_missing_docs_attrs(cx, &v.node.attrs, v.span, "a variant");
205     }
206 }