]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/missing_doc.rs
Version checks are useless now that we ride the trains
[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::{declare_lint, lint_array};
24 use rustc::ty;
25 use syntax::ast;
26 use syntax::attr;
27 use syntax::codemap::Span;
28 use crate::utils::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             cx.span_lint(
91                 MISSING_DOCS_IN_PRIVATE_ITEMS,
92                 sp,
93                 &format!("missing documentation for {}", desc),
94             );
95         }
96     }
97 }
98
99 impl LintPass for MissingDoc {
100     fn get_lints(&self) -> LintArray {
101         lint_array![MISSING_DOCS_IN_PRIVATE_ITEMS]
102     }
103 }
104
105 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDoc {
106     fn enter_lint_attrs(&mut self, _: &LateContext<'a, 'tcx>, attrs: &'tcx [ast::Attribute]) {
107         let doc_hidden = self.doc_hidden() || attrs.iter().any(|attr| {
108             attr.check_name("doc") && match attr.meta_item_list() {
109                 None => false,
110                 Some(l) => attr::list_contains_name(&l[..], "hidden"),
111             }
112         });
113         self.doc_hidden_stack.push(doc_hidden);
114     }
115
116     fn exit_lint_attrs(&mut self, _: &LateContext<'a, 'tcx>, _: &'tcx [ast::Attribute]) {
117         self.doc_hidden_stack.pop().expect("empty doc_hidden_stack");
118     }
119
120     fn check_crate(&mut self, cx: &LateContext<'a, 'tcx>, krate: &'tcx hir::Crate) {
121         self.check_missing_docs_attrs(cx, &krate.attrs, krate.span, "crate");
122     }
123
124     fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, it: &'tcx hir::Item) {
125         let desc = match it.node {
126             hir::ItemKind::Const(..) => "a constant",
127             hir::ItemKind::Enum(..) => "an enum",
128             hir::ItemKind::Fn(..) => {
129                 // ignore main()
130                 if it.name == "main" {
131                     let def_id = cx.tcx.hir.local_def_id(it.id);
132                     let def_key = cx.tcx.hir.def_key(def_id);
133                     if def_key.parent == Some(hir::def_id::CRATE_DEF_INDEX) {
134                         return;
135                     }
136                 }
137                 "a function"
138             },
139             hir::ItemKind::Mod(..) => "a module",
140             hir::ItemKind::Static(..) => "a static",
141             hir::ItemKind::Struct(..) => "a struct",
142             hir::ItemKind::Trait(..) => "a trait",
143             hir::ItemKind::TraitAlias(..) => "a trait alias",
144             hir::ItemKind::GlobalAsm(..) => "an assembly blob",
145             hir::ItemKind::Ty(..) => "a type alias",
146             hir::ItemKind::Union(..) => "a union",
147             hir::ItemKind::Existential(..) => "an existential type",
148             hir::ItemKind::ExternCrate(..) |
149             hir::ItemKind::ForeignMod(..) |
150             hir::ItemKind::Impl(..) |
151             hir::ItemKind::Use(..) => return,
152         };
153
154         self.check_missing_docs_attrs(cx, &it.attrs, it.span, desc);
155     }
156
157     fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, trait_item: &'tcx hir::TraitItem) {
158         let desc = match trait_item.node {
159             hir::TraitItemKind::Const(..) => "an associated constant",
160             hir::TraitItemKind::Method(..) => "a trait method",
161             hir::TraitItemKind::Type(..) => "an associated type",
162         };
163
164         self.check_missing_docs_attrs(cx, &trait_item.attrs, trait_item.span, desc);
165     }
166
167     fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, impl_item: &'tcx hir::ImplItem) {
168         // If the method is an impl for a trait, don't doc.
169         let def_id = cx.tcx.hir.local_def_id(impl_item.id);
170         match cx.tcx.associated_item(def_id).container {
171             ty::TraitContainer(_) => return,
172             ty::ImplContainer(cid) => if cx.tcx.impl_trait_ref(cid).is_some() {
173                 return;
174             },
175         }
176
177         let desc = match impl_item.node {
178             hir::ImplItemKind::Const(..) => "an associated constant",
179             hir::ImplItemKind::Method(..) => "a method",
180             hir::ImplItemKind::Type(_) => "an associated type",
181             hir::ImplItemKind::Existential(_) => "an existential type",
182         };
183         self.check_missing_docs_attrs(cx, &impl_item.attrs, impl_item.span, desc);
184     }
185
186     fn check_struct_field(&mut self, cx: &LateContext<'a, 'tcx>, sf: &'tcx hir::StructField) {
187         if !sf.is_positional() {
188             self.check_missing_docs_attrs(cx, &sf.attrs, sf.span, "a struct field");
189         }
190     }
191
192     fn check_variant(&mut self, cx: &LateContext<'a, 'tcx>, v: &'tcx hir::Variant, _: &hir::Generics) {
193         self.check_missing_docs_attrs(cx, &v.node.attrs, v.span, "a variant");
194     }
195 }