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;
11 use rustc::lint::{LateContext, LateLintPass, LintArray, LintContext, LintPass};
13 use rustc::{declare_tool_lint, impl_lint_pass};
14 use syntax::ast::{self, MetaItem, MetaItemKind};
16 use syntax::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 ::std::default::Default for MissingDoc {
40 fn default() -> Self {
46 pub fn new() -> Self {
48 doc_hidden_stack: vec![false],
52 fn doc_hidden(&self) -> bool {
53 *self.doc_hidden_stack.last().expect("empty doc_hidden_stack")
56 fn has_include(meta: Option<MetaItem>) -> bool {
58 if let Some(meta) = meta;
59 if let MetaItemKind::List(list) = meta.kind;
60 if let Some(meta) = list.get(0);
61 if let Some(name) = meta.ident();
63 name.as_str() == "include"
70 fn check_missing_docs_attrs(
72 cx: &LateContext<'_, '_>,
73 attrs: &[ast::Attribute],
77 // If we're building a test harness, then warning about
78 // documentation is probably not really relevant right now.
79 if cx.sess().opts.test {
83 // `#[doc(hidden)]` disables missing_docs check.
84 if self.doc_hidden() {
88 if sp.from_expansion() {
94 .any(|a| a.check_name(sym!(doc)) && (a.is_value_str() || Self::has_include(a.meta())));
98 MISSING_DOCS_IN_PRIVATE_ITEMS,
100 &format!("missing documentation for {}", desc),
106 impl_lint_pass!(MissingDoc => [MISSING_DOCS_IN_PRIVATE_ITEMS]);
108 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDoc {
109 fn enter_lint_attrs(&mut self, _: &LateContext<'a, 'tcx>, attrs: &'tcx [ast::Attribute]) {
110 let doc_hidden = self.doc_hidden()
111 || attrs.iter().any(|attr| {
112 attr.check_name(sym!(doc))
113 && match attr.meta_item_list() {
115 Some(l) => attr::list_contains_name(&l[..], sym!(hidden)),
118 self.doc_hidden_stack.push(doc_hidden);
121 fn exit_lint_attrs(&mut self, _: &LateContext<'a, 'tcx>, _: &'tcx [ast::Attribute]) {
122 self.doc_hidden_stack.pop().expect("empty doc_hidden_stack");
125 fn check_crate(&mut self, cx: &LateContext<'a, 'tcx>, krate: &'tcx hir::Crate) {
126 self.check_missing_docs_attrs(cx, &krate.attrs, krate.span, "crate");
129 fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, it: &'tcx hir::Item) {
130 let desc = match it.kind {
131 hir::ItemKind::Const(..) => "a constant",
132 hir::ItemKind::Enum(..) => "an enum",
133 hir::ItemKind::Fn(..) => {
135 if it.ident.name == sym!(main) {
136 let def_id = cx.tcx.hir().local_def_id(it.hir_id);
137 let def_key = cx.tcx.hir().def_key(def_id);
138 if def_key.parent == Some(hir::def_id::CRATE_DEF_INDEX) {
144 hir::ItemKind::Mod(..) => "a module",
145 hir::ItemKind::Static(..) => "a static",
146 hir::ItemKind::Struct(..) => "a struct",
147 hir::ItemKind::Trait(..) => "a trait",
148 hir::ItemKind::TraitAlias(..) => "a trait alias",
149 hir::ItemKind::TyAlias(..) => "a type alias",
150 hir::ItemKind::Union(..) => "a union",
151 hir::ItemKind::OpaqueTy(..) => "an existential type",
152 hir::ItemKind::ExternCrate(..)
153 | hir::ItemKind::ForeignMod(..)
154 | hir::ItemKind::GlobalAsm(..)
155 | hir::ItemKind::Impl(..)
156 | hir::ItemKind::Use(..) => return,
159 self.check_missing_docs_attrs(cx, &it.attrs, it.span, desc);
162 fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, trait_item: &'tcx hir::TraitItem) {
163 let desc = match trait_item.kind {
164 hir::TraitItemKind::Const(..) => "an associated constant",
165 hir::TraitItemKind::Method(..) => "a trait method",
166 hir::TraitItemKind::Type(..) => "an associated type",
169 self.check_missing_docs_attrs(cx, &trait_item.attrs, trait_item.span, desc);
172 fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, impl_item: &'tcx hir::ImplItem) {
173 // If the method is an impl for a trait, don't doc.
174 let def_id = cx.tcx.hir().local_def_id(impl_item.hir_id);
175 match cx.tcx.associated_item(def_id).container {
176 ty::TraitContainer(_) => return,
177 ty::ImplContainer(cid) => {
178 if cx.tcx.impl_trait_ref(cid).is_some() {
184 let desc = match impl_item.kind {
185 hir::ImplItemKind::Const(..) => "an associated constant",
186 hir::ImplItemKind::Method(..) => "a method",
187 hir::ImplItemKind::TyAlias(_) => "an associated type",
188 hir::ImplItemKind::OpaqueTy(_) => "an existential type",
190 self.check_missing_docs_attrs(cx, &impl_item.attrs, impl_item.span, desc);
193 fn check_struct_field(&mut self, cx: &LateContext<'a, 'tcx>, sf: &'tcx hir::StructField) {
194 if !sf.is_positional() {
195 self.check_missing_docs_attrs(cx, &sf.attrs, sf.span, "a struct field");
199 fn check_variant(&mut self, cx: &LateContext<'a, 'tcx>, v: &'tcx hir::Variant) {
200 self.check_missing_docs_attrs(cx, &v.attrs, v.span, "a variant");