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.
11 // This file incorporates work covered by the following copyright and
13 // Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
14 // file at the top-level directory of this distribution and at
15 // http://rust-lang.org/COPYRIGHT.
17 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
18 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
19 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
20 // option. This file may not be copied, modified, or distributed
21 // except according to those terms.
24 // Note: More specifically this lint is largely inspired (aka copied) from
28 // [`missing_doc`]: https://github.com/rust-lang/rust/blob/d6d05904697d89099b55da3331155392f1db9c00/src/librustc_lint/builtin.rs#L246
31 use crate::rustc::hir;
32 use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass, LintContext};
33 use crate::rustc::{declare_tool_lint, lint_array};
35 use crate::syntax::ast;
36 use crate::syntax::attr;
37 use crate::syntax::source_map::Span;
38 use crate::utils::{span_lint, in_macro};
40 /// **What it does:** Warns if there is missing doc for any documentable item
41 /// (public or private).
43 /// **Why is this bad?** Doc is good. *rustc* has a `MISSING_DOCS`
44 /// allowed-by-default lint for
45 /// public members, but has no way to enforce documentation of private items.
46 /// This lint fixes that.
48 /// **Known problems:** None.
49 declare_clippy_lint! {
50 pub MISSING_DOCS_IN_PRIVATE_ITEMS,
52 "detects missing documentation for public and private members"
55 pub struct MissingDoc {
56 /// Stack of whether #[doc(hidden)] is set
57 /// at each level which has lint attributes.
58 doc_hidden_stack: Vec<bool>,
61 impl ::std::default::Default for MissingDoc {
62 fn default() -> Self {
68 pub fn new() -> Self {
70 doc_hidden_stack: vec![false],
74 fn doc_hidden(&self) -> bool {
75 *self.doc_hidden_stack
77 .expect("empty doc_hidden_stack")
80 fn check_missing_docs_attrs(&self, cx: &LateContext<'_, '_>, attrs: &[ast::Attribute], sp: Span, desc: &'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() {
98 .any(|a| a.is_value_str() && a.name() == "doc");
102 MISSING_DOCS_IN_PRIVATE_ITEMS,
104 &format!("missing documentation for {}", desc),
110 impl LintPass for MissingDoc {
111 fn get_lints(&self) -> LintArray {
112 lint_array![MISSING_DOCS_IN_PRIVATE_ITEMS]
116 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDoc {
117 fn enter_lint_attrs(&mut self, _: &LateContext<'a, 'tcx>, attrs: &'tcx [ast::Attribute]) {
118 let doc_hidden = self.doc_hidden() || attrs.iter().any(|attr| {
119 attr.check_name("doc") && match attr.meta_item_list() {
121 Some(l) => attr::list_contains_name(&l[..], "hidden"),
124 self.doc_hidden_stack.push(doc_hidden);
127 fn exit_lint_attrs(&mut self, _: &LateContext<'a, 'tcx>, _: &'tcx [ast::Attribute]) {
128 self.doc_hidden_stack.pop().expect("empty doc_hidden_stack");
131 fn check_crate(&mut self, cx: &LateContext<'a, 'tcx>, krate: &'tcx hir::Crate) {
132 self.check_missing_docs_attrs(cx, &krate.attrs, krate.span, "crate");
135 fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, it: &'tcx hir::Item) {
136 let desc = match it.node {
137 hir::ItemKind::Const(..) => "a constant",
138 hir::ItemKind::Enum(..) => "an enum",
139 hir::ItemKind::Fn(..) => {
141 if it.name == "main" {
142 let def_id = cx.tcx.hir.local_def_id(it.id);
143 let def_key = cx.tcx.hir.def_key(def_id);
144 if def_key.parent == Some(hir::def_id::CRATE_DEF_INDEX) {
150 hir::ItemKind::Mod(..) => "a module",
151 hir::ItemKind::Static(..) => "a static",
152 hir::ItemKind::Struct(..) => "a struct",
153 hir::ItemKind::Trait(..) => "a trait",
154 hir::ItemKind::TraitAlias(..) => "a trait alias",
155 hir::ItemKind::GlobalAsm(..) => "an assembly blob",
156 hir::ItemKind::Ty(..) => "a type alias",
157 hir::ItemKind::Union(..) => "a union",
158 hir::ItemKind::Existential(..) => "an existential type",
159 hir::ItemKind::ExternCrate(..) |
160 hir::ItemKind::ForeignMod(..) |
161 hir::ItemKind::Impl(..) |
162 hir::ItemKind::Use(..) => return,
165 self.check_missing_docs_attrs(cx, &it.attrs, it.span, desc);
168 fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, trait_item: &'tcx hir::TraitItem) {
169 let desc = match trait_item.node {
170 hir::TraitItemKind::Const(..) => "an associated constant",
171 hir::TraitItemKind::Method(..) => "a trait method",
172 hir::TraitItemKind::Type(..) => "an associated type",
175 self.check_missing_docs_attrs(cx, &trait_item.attrs, trait_item.span, desc);
178 fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, impl_item: &'tcx hir::ImplItem) {
179 // If the method is an impl for a trait, don't doc.
180 let def_id = cx.tcx.hir.local_def_id(impl_item.id);
181 match cx.tcx.associated_item(def_id).container {
182 ty::TraitContainer(_) => return,
183 ty::ImplContainer(cid) => if cx.tcx.impl_trait_ref(cid).is_some() {
188 let desc = match impl_item.node {
189 hir::ImplItemKind::Const(..) => "an associated constant",
190 hir::ImplItemKind::Method(..) => "a method",
191 hir::ImplItemKind::Type(_) => "an associated type",
192 hir::ImplItemKind::Existential(_) => "an existential type",
194 self.check_missing_docs_attrs(cx, &impl_item.attrs, impl_item.span, desc);
197 fn check_struct_field(&mut self, cx: &LateContext<'a, 'tcx>, sf: &'tcx hir::StructField) {
198 if !sf.is_positional() {
199 self.check_missing_docs_attrs(cx, &sf.attrs, sf.span, "a struct field");
203 fn check_variant(&mut self, cx: &LateContext<'a, 'tcx>, v: &'tcx hir::Variant, _: &hir::Generics) {
204 self.check_missing_docs_attrs(cx, &v.node.attrs, v.span, "a variant");