]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/missing_doc.rs
Rollup merge of #89839 - jkugelman:must-use-mem-ptr-functions, r=joshtriplett
[rust.git] / src / tools / clippy / 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 rustc_ast::ast;
11 use rustc_hir as hir;
12 use rustc_lint::{LateContext, LateLintPass, LintContext};
13 use rustc_middle::ty;
14 use rustc_session::{declare_tool_lint, impl_lint_pass};
15 use rustc_span::def_id::CRATE_DEF_ID;
16 use rustc_span::source_map::Span;
17 use rustc_span::sym;
18
19 declare_clippy_lint! {
20     /// ### What it does
21     /// Warns if there is missing doc for any documentable item
22     /// (public or private).
23     ///
24     /// ### Why is this bad?
25     /// Doc is good. *rustc* has a `MISSING_DOCS`
26     /// allowed-by-default lint for
27     /// public members, but has no way to enforce documentation of private items.
28     /// This lint fixes that.
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 check_missing_docs_attrs(
60         &self,
61         cx: &LateContext<'_>,
62         attrs: &[ast::Attribute],
63         sp: Span,
64         article: &'static str,
65         desc: &'static str,
66     ) {
67         // If we're building a test harness, then warning about
68         // documentation is probably not really relevant right now.
69         if cx.sess().opts.test {
70             return;
71         }
72
73         // `#[doc(hidden)]` disables missing_docs check.
74         if self.doc_hidden() {
75             return;
76         }
77
78         if sp.from_expansion() {
79             return;
80         }
81
82         let has_doc = attrs.iter().any(|a| a.doc_str().is_some());
83         if !has_doc {
84             span_lint(
85                 cx,
86                 MISSING_DOCS_IN_PRIVATE_ITEMS,
87                 sp,
88                 &format!("missing documentation for {} {}", article, desc),
89             );
90         }
91     }
92 }
93
94 impl_lint_pass!(MissingDoc => [MISSING_DOCS_IN_PRIVATE_ITEMS]);
95
96 impl<'tcx> LateLintPass<'tcx> for MissingDoc {
97     fn enter_lint_attrs(&mut self, _: &LateContext<'tcx>, attrs: &'tcx [ast::Attribute]) {
98         let doc_hidden = self.doc_hidden() || is_doc_hidden(attrs);
99         self.doc_hidden_stack.push(doc_hidden);
100     }
101
102     fn exit_lint_attrs(&mut self, _: &LateContext<'tcx>, _: &'tcx [ast::Attribute]) {
103         self.doc_hidden_stack.pop().expect("empty doc_hidden_stack");
104     }
105
106     fn check_crate(&mut self, cx: &LateContext<'tcx>) {
107         let attrs = cx.tcx.hir().attrs(hir::CRATE_HIR_ID);
108         self.check_missing_docs_attrs(cx, attrs, cx.tcx.def_span(CRATE_DEF_ID), "the", "crate");
109     }
110
111     fn check_item(&mut self, cx: &LateContext<'tcx>, it: &'tcx hir::Item<'_>) {
112         match it.kind {
113             hir::ItemKind::Fn(..) => {
114                 // ignore main()
115                 if it.ident.name == sym::main {
116                     let def_key = cx.tcx.hir().def_key(it.def_id);
117                     if def_key.parent == Some(hir::def_id::CRATE_DEF_INDEX) {
118                         return;
119                     }
120                 }
121             },
122             hir::ItemKind::Const(..)
123             | hir::ItemKind::Enum(..)
124             | hir::ItemKind::Macro(..)
125             | hir::ItemKind::Mod(..)
126             | hir::ItemKind::Static(..)
127             | hir::ItemKind::Struct(..)
128             | hir::ItemKind::Trait(..)
129             | hir::ItemKind::TraitAlias(..)
130             | hir::ItemKind::TyAlias(..)
131             | hir::ItemKind::Union(..)
132             | hir::ItemKind::OpaqueTy(..) => {},
133             hir::ItemKind::ExternCrate(..)
134             | hir::ItemKind::ForeignMod { .. }
135             | hir::ItemKind::GlobalAsm(..)
136             | hir::ItemKind::Impl { .. }
137             | hir::ItemKind::Use(..) => return,
138         };
139
140         let (article, desc) = cx.tcx.article_and_description(it.def_id.to_def_id());
141
142         let attrs = cx.tcx.hir().attrs(it.hir_id());
143         self.check_missing_docs_attrs(cx, attrs, it.span, article, desc);
144     }
145
146     fn check_trait_item(&mut self, cx: &LateContext<'tcx>, trait_item: &'tcx hir::TraitItem<'_>) {
147         let (article, desc) = cx.tcx.article_and_description(trait_item.def_id.to_def_id());
148
149         let attrs = cx.tcx.hir().attrs(trait_item.hir_id());
150         self.check_missing_docs_attrs(cx, attrs, trait_item.span, article, desc);
151     }
152
153     fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx hir::ImplItem<'_>) {
154         // If the method is an impl for a trait, don't doc.
155         match cx.tcx.associated_item(impl_item.def_id).container {
156             ty::TraitContainer(_) => return,
157             ty::ImplContainer(cid) => {
158                 if cx.tcx.impl_trait_ref(cid).is_some() {
159                     return;
160                 }
161             },
162         }
163
164         let (article, desc) = cx.tcx.article_and_description(impl_item.def_id.to_def_id());
165         let attrs = cx.tcx.hir().attrs(impl_item.hir_id());
166         self.check_missing_docs_attrs(cx, attrs, impl_item.span, article, desc);
167     }
168
169     fn check_field_def(&mut self, cx: &LateContext<'tcx>, sf: &'tcx hir::FieldDef<'_>) {
170         if !sf.is_positional() {
171             let attrs = cx.tcx.hir().attrs(sf.hir_id);
172             self.check_missing_docs_attrs(cx, attrs, sf.span, "a", "struct field");
173         }
174     }
175
176     fn check_variant(&mut self, cx: &LateContext<'tcx>, v: &'tcx hir::Variant<'_>) {
177         let attrs = cx.tcx.hir().attrs(v.id);
178         self.check_missing_docs_attrs(cx, attrs, v.span, "a", "variant");
179     }
180 }