]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/missing_doc.rs
Auto merge of #81156 - DrMeepster:read_buf, 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     #[clippy::version = "pre 1.29.0"]
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 check_missing_docs_attrs(
61         &self,
62         cx: &LateContext<'_>,
63         attrs: &[ast::Attribute],
64         sp: Span,
65         article: &'static str,
66         desc: &'static str,
67     ) {
68         // If we're building a test harness, then warning about
69         // documentation is probably not really relevant right now.
70         if cx.sess().opts.test {
71             return;
72         }
73
74         // `#[doc(hidden)]` disables missing_docs check.
75         if self.doc_hidden() {
76             return;
77         }
78
79         if sp.from_expansion() {
80             return;
81         }
82
83         let has_doc = attrs.iter().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>) {
108         let attrs = cx.tcx.hir().attrs(hir::CRATE_HIR_ID);
109         self.check_missing_docs_attrs(cx, attrs, cx.tcx.def_span(CRATE_DEF_ID), "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::Macro(..)
126             | hir::ItemKind::Mod(..)
127             | hir::ItemKind::Static(..)
128             | hir::ItemKind::Struct(..)
129             | hir::ItemKind::Trait(..)
130             | hir::ItemKind::TraitAlias(..)
131             | hir::ItemKind::TyAlias(..)
132             | hir::ItemKind::Union(..)
133             | hir::ItemKind::OpaqueTy(..) => {},
134             hir::ItemKind::ExternCrate(..)
135             | hir::ItemKind::ForeignMod { .. }
136             | hir::ItemKind::GlobalAsm(..)
137             | hir::ItemKind::Impl { .. }
138             | hir::ItemKind::Use(..) => return,
139         };
140
141         let (article, desc) = cx.tcx.article_and_description(it.def_id.to_def_id());
142
143         let attrs = cx.tcx.hir().attrs(it.hir_id());
144         self.check_missing_docs_attrs(cx, attrs, it.span, article, desc);
145     }
146
147     fn check_trait_item(&mut self, cx: &LateContext<'tcx>, trait_item: &'tcx hir::TraitItem<'_>) {
148         let (article, desc) = cx.tcx.article_and_description(trait_item.def_id.to_def_id());
149
150         let attrs = cx.tcx.hir().attrs(trait_item.hir_id());
151         self.check_missing_docs_attrs(cx, attrs, trait_item.span, article, desc);
152     }
153
154     fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx hir::ImplItem<'_>) {
155         // If the method is an impl for a trait, don't doc.
156         match cx.tcx.associated_item(impl_item.def_id).container {
157             ty::TraitContainer(_) => return,
158             ty::ImplContainer(cid) => {
159                 if cx.tcx.impl_trait_ref(cid).is_some() {
160                     return;
161                 }
162             },
163         }
164
165         let (article, desc) = cx.tcx.article_and_description(impl_item.def_id.to_def_id());
166         let attrs = cx.tcx.hir().attrs(impl_item.hir_id());
167         self.check_missing_docs_attrs(cx, attrs, impl_item.span, article, desc);
168     }
169
170     fn check_field_def(&mut self, cx: &LateContext<'tcx>, sf: &'tcx hir::FieldDef<'_>) {
171         if !sf.is_positional() {
172             let attrs = cx.tcx.hir().attrs(sf.hir_id);
173             self.check_missing_docs_attrs(cx, attrs, sf.span, "a", "struct field");
174         }
175     }
176
177     fn check_variant(&mut self, cx: &LateContext<'tcx>, v: &'tcx hir::Variant<'_>) {
178         let attrs = cx.tcx.hir().attrs(v.id);
179         self.check_missing_docs_attrs(cx, attrs, v.span, "a", "variant");
180     }
181 }