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::{in_macro, span_lint};
10 use rustc::lint::{LateContext, LateLintPass, LintArray, LintContext, LintPass};
12 use rustc::{declare_tool_lint, lint_array};
15 use syntax::source_map::Span;
17 /// **What it does:** Warns if there is missing doc for any documentable item
18 /// (public or private).
20 /// **Why is this bad?** Doc is good. *rustc* has a `MISSING_DOCS`
21 /// allowed-by-default lint for
22 /// public members, but has no way to enforce documentation of private items.
23 /// This lint fixes that.
25 /// **Known problems:** None.
26 declare_clippy_lint! {
27 pub MISSING_DOCS_IN_PRIVATE_ITEMS,
29 "detects missing documentation for public and private members"
32 pub struct MissingDoc {
33 /// Stack of whether #[doc(hidden)] is set
34 /// at each level which has lint attributes.
35 doc_hidden_stack: Vec<bool>,
38 impl ::std::default::Default for MissingDoc {
39 fn default() -> Self {
45 pub fn new() -> Self {
47 doc_hidden_stack: vec![false],
51 fn doc_hidden(&self) -> bool {
52 *self.doc_hidden_stack.last().expect("empty doc_hidden_stack")
55 fn check_missing_docs_attrs(
57 cx: &LateContext<'_, '_>,
58 attrs: &[ast::Attribute],
62 // If we're building a test harness, then warning about
63 // documentation is probably not really relevant right now.
64 if cx.sess().opts.test {
68 // `#[doc(hidden)]` disables missing_docs check.
69 if self.doc_hidden() {
77 let has_doc = attrs.iter().any(|a| a.is_value_str() && a.name() == "doc");
81 MISSING_DOCS_IN_PRIVATE_ITEMS,
83 &format!("missing documentation for {}", desc),
89 impl LintPass for MissingDoc {
90 fn get_lints(&self) -> LintArray {
91 lint_array![MISSING_DOCS_IN_PRIVATE_ITEMS]
95 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDoc {
96 fn enter_lint_attrs(&mut self, _: &LateContext<'a, 'tcx>, attrs: &'tcx [ast::Attribute]) {
97 let doc_hidden = self.doc_hidden()
98 || attrs.iter().any(|attr| {
99 attr.check_name("doc")
100 && match attr.meta_item_list() {
102 Some(l) => attr::list_contains_name(&l[..], "hidden"),
105 self.doc_hidden_stack.push(doc_hidden);
108 fn exit_lint_attrs(&mut self, _: &LateContext<'a, 'tcx>, _: &'tcx [ast::Attribute]) {
109 self.doc_hidden_stack.pop().expect("empty doc_hidden_stack");
112 fn check_crate(&mut self, cx: &LateContext<'a, 'tcx>, krate: &'tcx hir::Crate) {
113 self.check_missing_docs_attrs(cx, &krate.attrs, krate.span, "crate");
116 fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, it: &'tcx hir::Item) {
117 let desc = match it.node {
118 hir::ItemKind::Const(..) => "a constant",
119 hir::ItemKind::Enum(..) => "an enum",
120 hir::ItemKind::Fn(..) => {
122 if it.ident.name == "main" {
123 let def_id = cx.tcx.hir().local_def_id(it.id);
124 let def_key = cx.tcx.hir().def_key(def_id);
125 if def_key.parent == Some(hir::def_id::CRATE_DEF_INDEX) {
131 hir::ItemKind::Mod(..) => "a module",
132 hir::ItemKind::Static(..) => "a static",
133 hir::ItemKind::Struct(..) => "a struct",
134 hir::ItemKind::Trait(..) => "a trait",
135 hir::ItemKind::TraitAlias(..) => "a trait alias",
136 hir::ItemKind::Ty(..) => "a type alias",
137 hir::ItemKind::Union(..) => "a union",
138 hir::ItemKind::Existential(..) => "an existential type",
139 hir::ItemKind::ExternCrate(..)
140 | hir::ItemKind::ForeignMod(..)
141 | hir::ItemKind::GlobalAsm(..)
142 | hir::ItemKind::Impl(..)
143 | hir::ItemKind::Use(..) => return,
146 self.check_missing_docs_attrs(cx, &it.attrs, it.span, desc);
149 fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, trait_item: &'tcx hir::TraitItem) {
150 let desc = match trait_item.node {
151 hir::TraitItemKind::Const(..) => "an associated constant",
152 hir::TraitItemKind::Method(..) => "a trait method",
153 hir::TraitItemKind::Type(..) => "an associated type",
156 self.check_missing_docs_attrs(cx, &trait_item.attrs, trait_item.span, desc);
159 fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, impl_item: &'tcx hir::ImplItem) {
160 // If the method is an impl for a trait, don't doc.
161 let def_id = cx.tcx.hir().local_def_id(impl_item.id);
162 match cx.tcx.associated_item(def_id).container {
163 ty::TraitContainer(_) => return,
164 ty::ImplContainer(cid) => {
165 if cx.tcx.impl_trait_ref(cid).is_some() {
171 let desc = match impl_item.node {
172 hir::ImplItemKind::Const(..) => "an associated constant",
173 hir::ImplItemKind::Method(..) => "a method",
174 hir::ImplItemKind::Type(_) => "an associated type",
175 hir::ImplItemKind::Existential(_) => "an existential type",
177 self.check_missing_docs_attrs(cx, &impl_item.attrs, impl_item.span, desc);
180 fn check_struct_field(&mut self, cx: &LateContext<'a, 'tcx>, sf: &'tcx hir::StructField) {
181 if !sf.is_positional() {
182 self.check_missing_docs_attrs(cx, &sf.attrs, sf.span, "a struct field");
186 fn check_variant(&mut self, cx: &LateContext<'a, 'tcx>, v: &'tcx hir::Variant, _: &hir::Generics) {
187 self.check_missing_docs_attrs(cx, &v.node.attrs, v.span, "a variant");