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