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