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