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