]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/missing_doc.rs
Merge commit 'b20d4c155d2fe3a8391f86dcf9a8c49e17188703' into clippyup
[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_ast::ast::{self, MetaItem, MetaItemKind};
11 use rustc_ast::attr;
12 use rustc_hir as hir;
13 use rustc_lint::{LateContext, LateLintPass, LintContext};
14 use rustc_middle::ty;
15 use rustc_session::{declare_tool_lint, impl_lint_pass};
16 use rustc_span::source_map::Span;
17 use rustc_span::sym;
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 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(&self, cx: &LateContext<'_>, attrs: &[ast::Attribute], sp: Span, desc: &'static str) {
74         // If we're building a test harness, then warning about
75         // documentation is probably not really relevant right now.
76         if cx.sess().opts.test {
77             return;
78         }
79
80         // `#[doc(hidden)]` disables missing_docs check.
81         if self.doc_hidden() {
82             return;
83         }
84
85         if sp.from_expansion() {
86             return;
87         }
88
89         let has_doc = attrs
90             .iter()
91             .any(|a| a.is_doc_comment() || a.doc_str().is_some() || a.is_value_str() || Self::has_include(a.meta()));
92         if !has_doc {
93             span_lint(
94                 cx,
95                 MISSING_DOCS_IN_PRIVATE_ITEMS,
96                 sp,
97                 &format!("missing documentation for {}", desc),
98             );
99         }
100     }
101 }
102
103 impl_lint_pass!(MissingDoc => [MISSING_DOCS_IN_PRIVATE_ITEMS]);
104
105 impl<'tcx> LateLintPass<'tcx> for MissingDoc {
106     fn enter_lint_attrs(&mut self, _: &LateContext<'tcx>, attrs: &'tcx [ast::Attribute]) {
107         let doc_hidden = self.doc_hidden()
108             || attrs.iter().any(|attr| {
109                 attr.has_name(sym::doc)
110                     && match attr.meta_item_list() {
111                         None => false,
112                         Some(l) => attr::list_contains_name(&l[..], sym::hidden),
113                     }
114             });
115         self.doc_hidden_stack.push(doc_hidden);
116     }
117
118     fn exit_lint_attrs(&mut self, _: &LateContext<'tcx>, _: &'tcx [ast::Attribute]) {
119         self.doc_hidden_stack.pop().expect("empty doc_hidden_stack");
120     }
121
122     fn check_crate(&mut self, cx: &LateContext<'tcx>, krate: &'tcx hir::Crate<'_>) {
123         self.check_missing_docs_attrs(cx, &krate.item.attrs, krate.item.span, "crate");
124     }
125
126     fn check_item(&mut self, cx: &LateContext<'tcx>, it: &'tcx hir::Item<'_>) {
127         let desc = match it.kind {
128             hir::ItemKind::Const(..) => "a constant",
129             hir::ItemKind::Enum(..) => "an enum",
130             hir::ItemKind::Fn(..) => {
131                 // ignore main()
132                 if it.ident.name == sym::main {
133                     let def_id = it.hir_id.owner;
134                     let def_key = cx.tcx.hir().def_key(def_id);
135                     if def_key.parent == Some(hir::def_id::CRATE_DEF_INDEX) {
136                         return;
137                     }
138                 }
139                 "a function"
140             },
141             hir::ItemKind::Mod(..) => "a module",
142             hir::ItemKind::Static(..) => "a static",
143             hir::ItemKind::Struct(..) => "a struct",
144             hir::ItemKind::Trait(..) => "a trait",
145             hir::ItemKind::TraitAlias(..) => "a trait alias",
146             hir::ItemKind::TyAlias(..) => "a type alias",
147             hir::ItemKind::Union(..) => "a union",
148             hir::ItemKind::OpaqueTy(..) => "an existential type",
149             hir::ItemKind::ExternCrate(..)
150             | hir::ItemKind::ForeignMod(..)
151             | hir::ItemKind::GlobalAsm(..)
152             | hir::ItemKind::Impl { .. }
153             | hir::ItemKind::Use(..) => return,
154         };
155
156         self.check_missing_docs_attrs(cx, &it.attrs, it.span, desc);
157     }
158
159     fn check_trait_item(&mut self, cx: &LateContext<'tcx>, trait_item: &'tcx hir::TraitItem<'_>) {
160         let desc = match trait_item.kind {
161             hir::TraitItemKind::Const(..) => "an associated constant",
162             hir::TraitItemKind::Fn(..) => "a trait method",
163             hir::TraitItemKind::Type(..) => "an associated type",
164         };
165
166         self.check_missing_docs_attrs(cx, &trait_item.attrs, trait_item.span, desc);
167     }
168
169     fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx hir::ImplItem<'_>) {
170         // If the method is an impl for a trait, don't doc.
171         let def_id = cx.tcx.hir().local_def_id(impl_item.hir_id);
172         match cx.tcx.associated_item(def_id).container {
173             ty::TraitContainer(_) => return,
174             ty::ImplContainer(cid) => {
175                 if cx.tcx.impl_trait_ref(cid).is_some() {
176                     return;
177                 }
178             },
179         }
180
181         let desc = match impl_item.kind {
182             hir::ImplItemKind::Const(..) => "an associated constant",
183             hir::ImplItemKind::Fn(..) => "a method",
184             hir::ImplItemKind::TyAlias(_) => "an associated type",
185         };
186         self.check_missing_docs_attrs(cx, &impl_item.attrs, impl_item.span, desc);
187     }
188
189     fn check_struct_field(&mut self, cx: &LateContext<'tcx>, sf: &'tcx hir::StructField<'_>) {
190         if !sf.is_positional() {
191             self.check_missing_docs_attrs(cx, &sf.attrs, sf.span, "a struct field");
192         }
193     }
194
195     fn check_variant(&mut self, cx: &LateContext<'tcx>, v: &'tcx hir::Variant<'_>) {
196         self.check_missing_docs_attrs(cx, &v.attrs, v.span, "a variant");
197     }
198 }