]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/missing_doc.rs
Auto merge of #83484 - JulianKnodt:infer, r=oli-obk,lcnr
[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 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::source_map::Span;
16 use rustc_span::sym;
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 check_missing_docs_attrs(
59         &self,
60         cx: &LateContext<'_>,
61         attrs: &[ast::Attribute],
62         sp: Span,
63         article: &'static str,
64         desc: &'static str,
65     ) {
66         // If we're building a test harness, then warning about
67         // documentation is probably not really relevant right now.
68         if cx.sess().opts.test {
69             return;
70         }
71
72         // `#[doc(hidden)]` disables missing_docs check.
73         if self.doc_hidden() {
74             return;
75         }
76
77         if sp.from_expansion() {
78             return;
79         }
80
81         let has_doc = attrs
82             .iter()
83             .any(|a| a.doc_str().is_some());
84         if !has_doc {
85             span_lint(
86                 cx,
87                 MISSING_DOCS_IN_PRIVATE_ITEMS,
88                 sp,
89                 &format!("missing documentation for {} {}", article, desc),
90             );
91         }
92     }
93 }
94
95 impl_lint_pass!(MissingDoc => [MISSING_DOCS_IN_PRIVATE_ITEMS]);
96
97 impl<'tcx> LateLintPass<'tcx> for MissingDoc {
98     fn enter_lint_attrs(&mut self, _: &LateContext<'tcx>, attrs: &'tcx [ast::Attribute]) {
99         let doc_hidden = self.doc_hidden() || is_doc_hidden(attrs);
100         self.doc_hidden_stack.push(doc_hidden);
101     }
102
103     fn exit_lint_attrs(&mut self, _: &LateContext<'tcx>, _: &'tcx [ast::Attribute]) {
104         self.doc_hidden_stack.pop().expect("empty doc_hidden_stack");
105     }
106
107     fn check_crate(&mut self, cx: &LateContext<'tcx>, krate: &'tcx hir::Crate<'_>) {
108         let attrs = cx.tcx.hir().attrs(hir::CRATE_HIR_ID);
109         self.check_missing_docs_attrs(cx, attrs, krate.module().inner, "the", "crate");
110     }
111
112     fn check_item(&mut self, cx: &LateContext<'tcx>, it: &'tcx hir::Item<'_>) {
113         match it.kind {
114             hir::ItemKind::Fn(..) => {
115                 // ignore main()
116                 if it.ident.name == sym::main {
117                     let def_key = cx.tcx.hir().def_key(it.def_id);
118                     if def_key.parent == Some(hir::def_id::CRATE_DEF_INDEX) {
119                         return;
120                     }
121                 }
122             },
123             hir::ItemKind::Const(..)
124             | hir::ItemKind::Enum(..)
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 }