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