1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
10 // This file incorporates work covered by the following copyright and
12 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
13 // file at the top-level directory of this distribution and at
14 // http://rust-lang.org/COPYRIGHT.
16 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
17 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
18 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
19 // option. This file may not be copied, modified, or distributed
20 // except according to those terms.
23 // Note: More specifically this lint is largely inspired (aka copied) from
27 // [`missing_doc`]: https://github.com/rust-lang/rust/blob/d6d05904697d89099b55da3331155392f1db9c00/src/librustc_lint/builtin.rs#L246
30 use crate::rustc::hir;
31 use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintContext, LintPass};
33 use crate::rustc::{declare_tool_lint, lint_array};
34 use crate::syntax::ast;
35 use crate::syntax::attr;
36 use crate::syntax::source_map::Span;
37 use crate::utils::{in_macro, span_lint};
39 /// **What it does:** Warns if there is missing doc for any documentable item
40 /// (public or private).
42 /// **Why is this bad?** Doc is good. *rustc* has a `MISSING_DOCS`
43 /// allowed-by-default lint for
44 /// public members, but has no way to enforce documentation of private items.
45 /// This lint fixes that.
47 /// **Known problems:** None.
48 declare_clippy_lint! {
49 pub MISSING_DOCS_IN_PRIVATE_ITEMS,
51 "detects missing documentation for public and private members"
54 pub struct MissingDoc {
55 /// Stack of whether #[doc(hidden)] is set
56 /// at each level which has lint attributes.
57 doc_hidden_stack: Vec<bool>,
60 impl ::std::default::Default for MissingDoc {
61 fn default() -> Self {
67 pub fn new() -> Self {
69 doc_hidden_stack: vec![false],
73 fn doc_hidden(&self) -> bool {
74 *self.doc_hidden_stack.last().expect("empty doc_hidden_stack")
77 fn check_missing_docs_attrs(
79 cx: &LateContext<'_, '_>,
80 attrs: &[ast::Attribute],
84 // If we're building a test harness, then warning about
85 // documentation is probably not really relevant right now.
86 if cx.sess().opts.test {
90 // `#[doc(hidden)]` disables missing_docs check.
91 if self.doc_hidden() {
99 let has_doc = attrs.iter().any(|a| a.is_value_str() && a.name() == "doc");
103 MISSING_DOCS_IN_PRIVATE_ITEMS,
105 &format!("missing documentation for {}", desc),
111 impl LintPass for MissingDoc {
112 fn get_lints(&self) -> LintArray {
113 lint_array![MISSING_DOCS_IN_PRIVATE_ITEMS]
117 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDoc {
118 fn enter_lint_attrs(&mut self, _: &LateContext<'a, 'tcx>, attrs: &'tcx [ast::Attribute]) {
119 let doc_hidden = self.doc_hidden()
120 || attrs.iter().any(|attr| {
121 attr.check_name("doc")
122 && match attr.meta_item_list() {
124 Some(l) => attr::list_contains_name(&l[..], "hidden"),
127 self.doc_hidden_stack.push(doc_hidden);
130 fn exit_lint_attrs(&mut self, _: &LateContext<'a, 'tcx>, _: &'tcx [ast::Attribute]) {
131 self.doc_hidden_stack.pop().expect("empty doc_hidden_stack");
134 fn check_crate(&mut self, cx: &LateContext<'a, 'tcx>, krate: &'tcx hir::Crate) {
135 self.check_missing_docs_attrs(cx, &krate.attrs, krate.span, "crate");
138 fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, it: &'tcx hir::Item) {
139 let desc = match it.node {
140 hir::ItemKind::Const(..) => "a constant",
141 hir::ItemKind::Enum(..) => "an enum",
142 hir::ItemKind::Fn(..) => {
144 if it.name == "main" {
145 let def_id = cx.tcx.hir().local_def_id(it.id);
146 let def_key = cx.tcx.hir().def_key(def_id);
147 if def_key.parent == Some(hir::def_id::CRATE_DEF_INDEX) {
153 hir::ItemKind::Mod(..) => "a module",
154 hir::ItemKind::Static(..) => "a static",
155 hir::ItemKind::Struct(..) => "a struct",
156 hir::ItemKind::Trait(..) => "a trait",
157 hir::ItemKind::TraitAlias(..) => "a trait alias",
158 hir::ItemKind::GlobalAsm(..) => "an assembly blob",
159 hir::ItemKind::Ty(..) => "a type alias",
160 hir::ItemKind::Union(..) => "a union",
161 hir::ItemKind::Existential(..) => "an existential type",
162 hir::ItemKind::ExternCrate(..)
163 | hir::ItemKind::ForeignMod(..)
164 | hir::ItemKind::Impl(..)
165 | hir::ItemKind::Use(..) => return,
168 self.check_missing_docs_attrs(cx, &it.attrs, it.span, desc);
171 fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, trait_item: &'tcx hir::TraitItem) {
172 let desc = match trait_item.node {
173 hir::TraitItemKind::Const(..) => "an associated constant",
174 hir::TraitItemKind::Method(..) => "a trait method",
175 hir::TraitItemKind::Type(..) => "an associated type",
178 self.check_missing_docs_attrs(cx, &trait_item.attrs, trait_item.span, desc);
181 fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, impl_item: &'tcx hir::ImplItem) {
182 // If the method is an impl for a trait, don't doc.
183 let def_id = cx.tcx.hir().local_def_id(impl_item.id);
184 match cx.tcx.associated_item(def_id).container {
185 ty::TraitContainer(_) => return,
186 ty::ImplContainer(cid) => {
187 if cx.tcx.impl_trait_ref(cid).is_some() {
193 let desc = match impl_item.node {
194 hir::ImplItemKind::Const(..) => "an associated constant",
195 hir::ImplItemKind::Method(..) => "a method",
196 hir::ImplItemKind::Type(_) => "an associated type",
197 hir::ImplItemKind::Existential(_) => "an existential type",
199 self.check_missing_docs_attrs(cx, &impl_item.attrs, impl_item.span, desc);
202 fn check_struct_field(&mut self, cx: &LateContext<'a, 'tcx>, sf: &'tcx hir::StructField) {
203 if !sf.is_positional() {
204 self.check_missing_docs_attrs(cx, &sf.attrs, sf.span, "a struct field");
208 fn check_variant(&mut self, cx: &LateContext<'a, 'tcx>, v: &'tcx hir::Variant, _: &hir::Generics) {
209 self.check_missing_docs_attrs(cx, &v.node.attrs, v.span, "a variant");