1 // Note: More specifically this lint is largely inspired (aka copied) from
5 // [`missing_doc`]: https://github.com/rust-lang/rust/blob/d6d05904697d89099b55da3331155392f1db9c00/src/librustc_lint/builtin.rs#L246
8 use crate::utils::span_lint;
9 use if_chain::if_chain;
10 use rustc_ast::ast::{self, MetaItem, MetaItemKind};
13 use rustc_lint::{LateContext, LateLintPass, LintContext};
15 use rustc_session::{declare_tool_lint, impl_lint_pass};
16 use rustc_span::source_map::Span;
18 declare_clippy_lint! {
19 /// **What it does:** Warns if there is missing doc for any documentable item
20 /// (public or private).
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.
27 /// **Known problems:** None.
28 pub MISSING_DOCS_IN_PRIVATE_ITEMS,
30 "detects missing documentation for public and private members"
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>,
39 impl Default for MissingDoc {
41 fn default() -> Self {
48 pub fn new() -> Self {
50 doc_hidden_stack: vec![false],
54 fn doc_hidden(&self) -> bool {
55 *self.doc_hidden_stack.last().expect("empty doc_hidden_stack")
58 fn has_include(meta: Option<MetaItem>) -> bool {
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();
65 name.as_str() == "include"
72 fn check_missing_docs_attrs(&self, cx: &LateContext<'_>, attrs: &[ast::Attribute], sp: Span, desc: &'static str) {
73 // If we're building a test harness, then warning about
74 // documentation is probably not really relevant right now.
75 if cx.sess().opts.test {
79 // `#[doc(hidden)]` disables missing_docs check.
80 if self.doc_hidden() {
84 if sp.from_expansion() {
90 .any(|a| a.is_doc_comment() || a.doc_str().is_some() || a.is_value_str() || Self::has_include(a.meta()));
94 MISSING_DOCS_IN_PRIVATE_ITEMS,
96 &format!("missing documentation for {}", desc),
102 impl_lint_pass!(MissingDoc => [MISSING_DOCS_IN_PRIVATE_ITEMS]);
104 impl<'tcx> LateLintPass<'tcx> for MissingDoc {
105 fn enter_lint_attrs(&mut self, _: &LateContext<'tcx>, attrs: &'tcx [ast::Attribute]) {
106 let doc_hidden = self.doc_hidden()
107 || attrs.iter().any(|attr| {
108 attr.has_name(sym!(doc))
109 && match attr.meta_item_list() {
111 Some(l) => attr::list_contains_name(&l[..], sym!(hidden)),
114 self.doc_hidden_stack.push(doc_hidden);
117 fn exit_lint_attrs(&mut self, _: &LateContext<'tcx>, _: &'tcx [ast::Attribute]) {
118 self.doc_hidden_stack.pop().expect("empty doc_hidden_stack");
121 fn check_crate(&mut self, cx: &LateContext<'tcx>, krate: &'tcx hir::Crate<'_>) {
122 self.check_missing_docs_attrs(cx, &krate.item.attrs, krate.item.span, "crate");
125 fn check_item(&mut self, cx: &LateContext<'tcx>, it: &'tcx hir::Item<'_>) {
126 let desc = match it.kind {
127 hir::ItemKind::Const(..) => "a constant",
128 hir::ItemKind::Enum(..) => "an enum",
129 hir::ItemKind::Fn(..) => {
131 if it.ident.name == sym!(main) {
132 let def_id = it.hir_id.owner;
133 let def_key = cx.tcx.hir().def_key(def_id);
134 if def_key.parent == Some(hir::def_id::CRATE_DEF_INDEX) {
140 hir::ItemKind::Mod(..) => "a module",
141 hir::ItemKind::Static(..) => "a static",
142 hir::ItemKind::Struct(..) => "a struct",
143 hir::ItemKind::Trait(..) => "a trait",
144 hir::ItemKind::TraitAlias(..) => "a trait alias",
145 hir::ItemKind::TyAlias(..) => "a type alias",
146 hir::ItemKind::Union(..) => "a union",
147 hir::ItemKind::OpaqueTy(..) => "an existential type",
148 hir::ItemKind::ExternCrate(..)
149 | hir::ItemKind::ForeignMod(..)
150 | hir::ItemKind::GlobalAsm(..)
151 | hir::ItemKind::Impl { .. }
152 | hir::ItemKind::Use(..) => return,
155 self.check_missing_docs_attrs(cx, &it.attrs, it.span, desc);
158 fn check_trait_item(&mut self, cx: &LateContext<'tcx>, trait_item: &'tcx hir::TraitItem<'_>) {
159 let desc = match trait_item.kind {
160 hir::TraitItemKind::Const(..) => "an associated constant",
161 hir::TraitItemKind::Fn(..) => "a trait method",
162 hir::TraitItemKind::Type(..) => "an associated type",
165 self.check_missing_docs_attrs(cx, &trait_item.attrs, trait_item.span, desc);
168 fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx hir::ImplItem<'_>) {
169 // If the method is an impl for a trait, don't doc.
170 let def_id = cx.tcx.hir().local_def_id(impl_item.hir_id);
171 match cx.tcx.associated_item(def_id).container {
172 ty::TraitContainer(_) => return,
173 ty::ImplContainer(cid) => {
174 if cx.tcx.impl_trait_ref(cid).is_some() {
180 let desc = match impl_item.kind {
181 hir::ImplItemKind::Const(..) => "an associated constant",
182 hir::ImplItemKind::Fn(..) => "a method",
183 hir::ImplItemKind::TyAlias(_) => "an associated type",
185 self.check_missing_docs_attrs(cx, &impl_item.attrs, impl_item.span, desc);
188 fn check_struct_field(&mut self, cx: &LateContext<'tcx>, sf: &'tcx hir::StructField<'_>) {
189 if !sf.is_positional() {
190 self.check_missing_docs_attrs(cx, &sf.attrs, sf.span, "a struct field");
194 fn check_variant(&mut self, cx: &LateContext<'tcx>, v: &'tcx hir::Variant<'_>) {
195 self.check_missing_docs_attrs(cx, &v.attrs, v.span, "a variant");