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