1 // Note: More specifically this lint is largely inspired (aka copied) from
5 // [`missing_doc`]: https://github.com/rust-lang/rust/blob/cf9cf7c923eb01146971429044f216a3ca905e06/compiler/rustc_lint/src/builtin.rs#L415
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;
19 declare_clippy_lint! {
20 /// **What it does:** Warns if there is missing doc for any documentable item
21 /// (public or private).
23 /// **Why is this bad?** Doc is good. *rustc* has a `MISSING_DOCS`
24 /// allowed-by-default lint for
25 /// public members, but has no way to enforce documentation of private items.
26 /// This lint fixes that.
28 /// **Known problems:** None.
29 pub MISSING_DOCS_IN_PRIVATE_ITEMS,
31 "detects missing documentation for public and private members"
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>,
40 impl Default for MissingDoc {
42 fn default() -> Self {
49 pub fn new() -> Self {
51 doc_hidden_stack: vec![false],
55 fn doc_hidden(&self) -> bool {
56 *self.doc_hidden_stack.last().expect("empty doc_hidden_stack")
59 fn has_include(meta: Option<MetaItem>) -> bool {
61 if let Some(meta) = meta;
62 if let MetaItemKind::List(list) = meta.kind;
63 if let Some(meta) = list.get(0);
64 if let Some(name) = meta.ident();
66 name.as_str() == "include"
73 fn check_missing_docs_attrs(
76 attrs: &[ast::Attribute],
78 article: &'static str,
81 // If we're building a test harness, then warning about
82 // documentation is probably not really relevant right now.
83 if cx.sess().opts.test {
87 // `#[doc(hidden)]` disables missing_docs check.
88 if self.doc_hidden() {
92 if sp.from_expansion() {
98 .any(|a| a.is_doc_comment() || a.doc_str().is_some() || a.is_value_str() || Self::has_include(a.meta()));
102 MISSING_DOCS_IN_PRIVATE_ITEMS,
104 &format!("missing documentation for {} {}", article, desc),
110 impl_lint_pass!(MissingDoc => [MISSING_DOCS_IN_PRIVATE_ITEMS]);
112 impl<'tcx> LateLintPass<'tcx> for MissingDoc {
113 fn enter_lint_attrs(&mut self, _: &LateContext<'tcx>, attrs: &'tcx [ast::Attribute]) {
114 let doc_hidden = self.doc_hidden()
115 || attrs.iter().any(|attr| {
116 attr.has_name(sym::doc)
117 && match attr.meta_item_list() {
119 Some(l) => attr::list_contains_name(&l[..], sym::hidden),
122 self.doc_hidden_stack.push(doc_hidden);
125 fn exit_lint_attrs(&mut self, _: &LateContext<'tcx>, _: &'tcx [ast::Attribute]) {
126 self.doc_hidden_stack.pop().expect("empty doc_hidden_stack");
129 fn check_crate(&mut self, cx: &LateContext<'tcx>, krate: &'tcx hir::Crate<'_>) {
130 self.check_missing_docs_attrs(cx, &krate.item.attrs, krate.item.span, "the", "crate");
133 fn check_item(&mut self, cx: &LateContext<'tcx>, it: &'tcx hir::Item<'_>) {
135 hir::ItemKind::Fn(..) => {
137 if it.ident.name == sym::main {
138 let def_id = it.hir_id.owner;
139 let def_key = cx.tcx.hir().def_key(def_id);
140 if def_key.parent == Some(hir::def_id::CRATE_DEF_INDEX) {
145 hir::ItemKind::Const(..)
146 | hir::ItemKind::Enum(..)
147 | hir::ItemKind::Mod(..)
148 | hir::ItemKind::Static(..)
149 | hir::ItemKind::Struct(..)
150 | hir::ItemKind::Trait(..)
151 | hir::ItemKind::TraitAlias(..)
152 | hir::ItemKind::TyAlias(..)
153 | hir::ItemKind::Union(..)
154 | hir::ItemKind::OpaqueTy(..) => {},
155 hir::ItemKind::ExternCrate(..)
156 | hir::ItemKind::ForeignMod { .. }
157 | hir::ItemKind::GlobalAsm(..)
158 | hir::ItemKind::Impl { .. }
159 | hir::ItemKind::Use(..) => return,
162 let def_id = cx.tcx.hir().local_def_id(it.hir_id);
163 let (article, desc) = cx.tcx.article_and_description(def_id.to_def_id());
165 self.check_missing_docs_attrs(cx, &it.attrs, it.span, article, desc);
168 fn check_trait_item(&mut self, cx: &LateContext<'tcx>, trait_item: &'tcx hir::TraitItem<'_>) {
169 let def_id = cx.tcx.hir().local_def_id(trait_item.hir_id);
170 let (article, desc) = cx.tcx.article_and_description(def_id.to_def_id());
172 self.check_missing_docs_attrs(cx, &trait_item.attrs, trait_item.span, article, desc);
175 fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx hir::ImplItem<'_>) {
176 // If the method is an impl for a trait, don't doc.
177 let def_id = cx.tcx.hir().local_def_id(impl_item.hir_id);
178 match cx.tcx.associated_item(def_id).container {
179 ty::TraitContainer(_) => return,
180 ty::ImplContainer(cid) => {
181 if cx.tcx.impl_trait_ref(cid).is_some() {
187 let (article, desc) = cx.tcx.article_and_description(def_id.to_def_id());
188 self.check_missing_docs_attrs(cx, &impl_item.attrs, impl_item.span, article, desc);
191 fn check_struct_field(&mut self, cx: &LateContext<'tcx>, sf: &'tcx hir::StructField<'_>) {
192 if !sf.is_positional() {
193 self.check_missing_docs_attrs(cx, &sf.attrs, sf.span, "a", "struct field");
197 fn check_variant(&mut self, cx: &LateContext<'tcx>, v: &'tcx hir::Variant<'_>) {
198 self.check_missing_docs_attrs(cx, &v.attrs, v.span, "a", "variant");