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
26 use syntax::codemap::Span;
29 /// **What it does:** Warns if there is missing doc for any documentable item
30 /// (public or private).
32 /// **Why is this bad?** Doc is good. *rustc* has a `MISSING_DOCS`
33 /// allowed-by-default lint for
34 /// public members, but has no way to enforce documentation of private items.
35 /// This lint fixes that.
37 /// **Known problems:** None.
38 declare_clippy_lint! {
39 pub MISSING_DOCS_IN_PRIVATE_ITEMS,
41 "detects missing documentation for public and private members"
44 pub struct MissingDoc {
45 /// Stack of whether #[doc(hidden)] is set
46 /// at each level which has lint attributes.
47 doc_hidden_stack: Vec<bool>,
50 impl ::std::default::Default for MissingDoc {
51 fn default() -> Self {
57 pub fn new() -> Self {
59 doc_hidden_stack: vec![false],
63 fn doc_hidden(&self) -> bool {
64 *self.doc_hidden_stack
66 .expect("empty doc_hidden_stack")
69 fn check_missing_docs_attrs(&self, cx: &LateContext, attrs: &[ast::Attribute], sp: Span, desc: &'static str) {
70 // If we're building a test harness, then warning about
71 // documentation is probably not really relevant right now.
72 if cx.sess().opts.test {
76 // `#[doc(hidden)]` disables missing_docs check.
77 if self.doc_hidden() {
87 .any(|a| a.is_value_str() && a.name() == "doc");
90 MISSING_DOCS_IN_PRIVATE_ITEMS,
92 &format!("missing documentation for {}", desc),
98 impl LintPass for MissingDoc {
99 fn get_lints(&self) -> LintArray {
100 lint_array![MISSING_DOCS_IN_PRIVATE_ITEMS]
104 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDoc {
105 fn enter_lint_attrs(&mut self, _: &LateContext<'a, 'tcx>, attrs: &'tcx [ast::Attribute]) {
106 let doc_hidden = self.doc_hidden() || attrs.iter().any(|attr| {
107 attr.check_name("doc") && match attr.meta_item_list() {
109 Some(l) => attr::list_contains_name(&l[..], "hidden"),
112 self.doc_hidden_stack.push(doc_hidden);
115 fn exit_lint_attrs(&mut self, _: &LateContext<'a, 'tcx>, _: &'tcx [ast::Attribute]) {
116 self.doc_hidden_stack.pop().expect("empty doc_hidden_stack");
119 fn check_crate(&mut self, cx: &LateContext<'a, 'tcx>, krate: &'tcx hir::Crate) {
120 self.check_missing_docs_attrs(cx, &krate.attrs, krate.span, "crate");
123 fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, it: &'tcx hir::Item) {
124 let desc = match it.node {
125 hir::ItemConst(..) => "a constant",
126 hir::ItemEnum(..) => "an enum",
129 if it.name == "main" {
130 let def_id = cx.tcx.hir.local_def_id(it.id);
131 let def_key = cx.tcx.hir.def_key(def_id);
132 if def_key.parent == Some(hir::def_id::CRATE_DEF_INDEX) {
138 hir::ItemMod(..) => "a module",
139 hir::ItemStatic(..) => "a static",
140 hir::ItemStruct(..) => "a struct",
141 hir::ItemTrait(..) => "a trait",
142 hir::ItemTraitAlias(..) => "a trait alias",
143 hir::ItemGlobalAsm(..) => "an assembly blob",
144 hir::ItemTy(..) => "a type alias",
145 hir::ItemUnion(..) => "a union",
146 hir::ItemExternCrate(..) |
147 hir::ItemForeignMod(..) |
149 hir::ItemUse(..) => return,
152 self.check_missing_docs_attrs(cx, &it.attrs, it.span, desc);
155 fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, trait_item: &'tcx hir::TraitItem) {
156 let desc = match trait_item.node {
157 hir::TraitItemKind::Const(..) => "an associated constant",
158 hir::TraitItemKind::Method(..) => "a trait method",
159 hir::TraitItemKind::Type(..) => "an associated type",
162 self.check_missing_docs_attrs(cx, &trait_item.attrs, trait_item.span, desc);
165 fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, impl_item: &'tcx hir::ImplItem) {
166 // If the method is an impl for a trait, don't doc.
167 let def_id = cx.tcx.hir.local_def_id(impl_item.id);
168 match cx.tcx.associated_item(def_id).container {
169 ty::TraitContainer(_) => return,
170 ty::ImplContainer(cid) => if cx.tcx.impl_trait_ref(cid).is_some() {
175 let desc = match impl_item.node {
176 hir::ImplItemKind::Const(..) => "an associated constant",
177 hir::ImplItemKind::Method(..) => "a method",
178 hir::ImplItemKind::Type(_) => "an associated type",
180 self.check_missing_docs_attrs(cx, &impl_item.attrs, impl_item.span, desc);
183 fn check_struct_field(&mut self, cx: &LateContext<'a, 'tcx>, sf: &'tcx hir::StructField) {
184 if !sf.is_positional() {
185 self.check_missing_docs_attrs(cx, &sf.attrs, sf.span, "a struct field");
189 fn check_variant(&mut self, cx: &LateContext<'a, 'tcx>, v: &'tcx hir::Variant, _: &hir::Generics) {
190 self.check_missing_docs_attrs(cx, &v.node.attrs, v.span, "a variant");