1 // This file incorporates work covered by the following copyright and
3 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
4 // file at the top-level directory of this distribution and at
5 // http://rust-lang.org/COPYRIGHT.
7 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
8 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
9 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
10 // option. This file may not be copied, modified, or distributed
11 // except according to those terms.
14 // Note: More specifically this lint is largely inspired (aka copied) from
18 // [`missing_doc`]: https://github.com/rust-lang/rust/blob/d6d05904697d89099b55da3331155392f1db9c00/src/librustc_lint/builtin.rs#L246
23 use rustc::{declare_lint, lint_array};
27 use syntax::codemap::Span;
28 use crate::utils::in_macro;
30 /// **What it does:** Warns if there is missing doc for any documentable item
31 /// (public or private).
33 /// **Why is this bad?** Doc is good. *rustc* has a `MISSING_DOCS`
34 /// allowed-by-default lint for
35 /// public members, but has no way to enforce documentation of private items.
36 /// This lint fixes that.
38 /// **Known problems:** None.
39 declare_clippy_lint! {
40 pub MISSING_DOCS_IN_PRIVATE_ITEMS,
42 "detects missing documentation for public and private members"
45 pub struct MissingDoc {
46 /// Stack of whether #[doc(hidden)] is set
47 /// at each level which has lint attributes.
48 doc_hidden_stack: Vec<bool>,
51 impl ::std::default::Default for MissingDoc {
52 fn default() -> Self {
58 pub fn new() -> Self {
60 doc_hidden_stack: vec![false],
64 fn doc_hidden(&self) -> bool {
65 *self.doc_hidden_stack
67 .expect("empty doc_hidden_stack")
70 fn check_missing_docs_attrs(&self, cx: &LateContext<'_, '_>, attrs: &[ast::Attribute], sp: Span, desc: &'static str) {
71 // If we're building a test harness, then warning about
72 // documentation is probably not really relevant right now.
73 if cx.sess().opts.test {
77 // `#[doc(hidden)]` disables missing_docs check.
78 if self.doc_hidden() {
88 .any(|a| a.is_value_str() && a.name() == "doc");
91 MISSING_DOCS_IN_PRIVATE_ITEMS,
93 &format!("missing documentation for {}", desc),
99 impl LintPass for MissingDoc {
100 fn get_lints(&self) -> LintArray {
101 lint_array![MISSING_DOCS_IN_PRIVATE_ITEMS]
105 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDoc {
106 fn enter_lint_attrs(&mut self, _: &LateContext<'a, 'tcx>, attrs: &'tcx [ast::Attribute]) {
107 let doc_hidden = self.doc_hidden() || attrs.iter().any(|attr| {
108 attr.check_name("doc") && match attr.meta_item_list() {
110 Some(l) => attr::list_contains_name(&l[..], "hidden"),
113 self.doc_hidden_stack.push(doc_hidden);
116 fn exit_lint_attrs(&mut self, _: &LateContext<'a, 'tcx>, _: &'tcx [ast::Attribute]) {
117 self.doc_hidden_stack.pop().expect("empty doc_hidden_stack");
120 fn check_crate(&mut self, cx: &LateContext<'a, 'tcx>, krate: &'tcx hir::Crate) {
121 self.check_missing_docs_attrs(cx, &krate.attrs, krate.span, "crate");
124 fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, it: &'tcx hir::Item) {
125 let desc = match it.node {
126 hir::ItemKind::Const(..) => "a constant",
127 hir::ItemKind::Enum(..) => "an enum",
128 hir::ItemKind::Fn(..) => {
130 if it.name == "main" {
131 let def_id = cx.tcx.hir.local_def_id(it.id);
132 let def_key = cx.tcx.hir.def_key(def_id);
133 if def_key.parent == Some(hir::def_id::CRATE_DEF_INDEX) {
139 hir::ItemKind::Mod(..) => "a module",
140 hir::ItemKind::Static(..) => "a static",
141 hir::ItemKind::Struct(..) => "a struct",
142 hir::ItemKind::Trait(..) => "a trait",
143 hir::ItemKind::TraitAlias(..) => "a trait alias",
144 hir::ItemKind::GlobalAsm(..) => "an assembly blob",
145 hir::ItemKind::Ty(..) => "a type alias",
146 hir::ItemKind::Union(..) => "a union",
147 hir::ItemKind::Existential(..) => "an existential type",
148 hir::ItemKind::ExternCrate(..) |
149 hir::ItemKind::ForeignMod(..) |
150 hir::ItemKind::Impl(..) |
151 hir::ItemKind::Use(..) => return,
154 self.check_missing_docs_attrs(cx, &it.attrs, it.span, desc);
157 fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, trait_item: &'tcx hir::TraitItem) {
158 let desc = match trait_item.node {
159 hir::TraitItemKind::Const(..) => "an associated constant",
160 hir::TraitItemKind::Method(..) => "a trait method",
161 hir::TraitItemKind::Type(..) => "an associated type",
164 self.check_missing_docs_attrs(cx, &trait_item.attrs, trait_item.span, desc);
167 fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, impl_item: &'tcx hir::ImplItem) {
168 // If the method is an impl for a trait, don't doc.
169 let def_id = cx.tcx.hir.local_def_id(impl_item.id);
170 match cx.tcx.associated_item(def_id).container {
171 ty::TraitContainer(_) => return,
172 ty::ImplContainer(cid) => if cx.tcx.impl_trait_ref(cid).is_some() {
177 let desc = match impl_item.node {
178 hir::ImplItemKind::Const(..) => "an associated constant",
179 hir::ImplItemKind::Method(..) => "a method",
180 hir::ImplItemKind::Type(_) => "an associated type",
181 hir::ImplItemKind::Existential(_) => "an existential type",
183 self.check_missing_docs_attrs(cx, &impl_item.attrs, impl_item.span, desc);
186 fn check_struct_field(&mut self, cx: &LateContext<'a, 'tcx>, sf: &'tcx hir::StructField) {
187 if !sf.is_positional() {
188 self.check_missing_docs_attrs(cx, &sf.attrs, sf.span, "a struct field");
192 fn check_variant(&mut self, cx: &LateContext<'a, 'tcx>, v: &'tcx hir::Variant, _: &hir::Generics) {
193 self.check_missing_docs_attrs(cx, &v.node.attrs, v.span, "a variant");