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
20 // com/rust-lang/rust/blob/d6d05904697d89099b55da3331155392f1db9c00/src/librustc_lint/builtin.
33 use syntax::codemap::Span;
36 /// **What it does:** Warns if there is missing doc for any documentable item
37 /// (public or private).
39 /// **Why is this bad?** Doc is good. *rustc* has a `MISSING_DOCS`
40 /// allowed-by-default lint for
41 /// public members, but has no way to enforce documentation of private items.
42 /// This lint fixes that.
44 /// **Known problems:** None.
46 pub MISSING_DOCS_IN_PRIVATE_ITEMS,
48 "detects missing documentation for public and private members"
51 pub struct MissingDoc {
52 /// Stack of whether #[doc(hidden)] is set
53 /// at each level which has lint attributes.
54 doc_hidden_stack: Vec<bool>,
57 impl ::std::default::Default for MissingDoc {
58 fn default() -> MissingDoc {
64 pub fn new() -> MissingDoc {
65 MissingDoc { doc_hidden_stack: vec![false] }
68 fn doc_hidden(&self) -> bool {
69 *self.doc_hidden_stack.last().expect(
70 "empty doc_hidden_stack",
74 fn check_missing_docs_attrs(&self, cx: &LateContext, attrs: &[ast::Attribute], sp: Span, desc: &'static str) {
75 // If we're building a test harness, then warning about
76 // documentation is probably not really relevant right now.
77 if cx.sess().opts.test {
81 // `#[doc(hidden)]` disables missing_docs check.
82 if self.doc_hidden() {
90 let has_doc = attrs.iter().any(|a| {
91 a.is_value_str() && a.name().map_or(false, |n| n == "doc")
95 MISSING_DOCS_IN_PRIVATE_ITEMS,
97 &format!("missing documentation for {}", desc),
103 impl LintPass for MissingDoc {
104 fn get_lints(&self) -> LintArray {
105 lint_array![MISSING_DOCS_IN_PRIVATE_ITEMS]
109 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDoc {
110 fn enter_lint_attrs(&mut self, _: &LateContext<'a, 'tcx>, attrs: &'tcx [ast::Attribute]) {
111 let doc_hidden = self.doc_hidden() ||
112 attrs.iter().any(|attr| {
113 attr.check_name("doc") &&
114 match attr.meta_item_list() {
116 Some(l) => attr::list_contains_name(&l[..], "hidden"),
119 self.doc_hidden_stack.push(doc_hidden);
122 fn exit_lint_attrs(&mut self, _: &LateContext<'a, 'tcx>, _: &'tcx [ast::Attribute]) {
123 self.doc_hidden_stack.pop().expect("empty doc_hidden_stack");
126 fn check_crate(&mut self, cx: &LateContext<'a, 'tcx>, krate: &'tcx hir::Crate) {
127 self.check_missing_docs_attrs(cx, &krate.attrs, krate.span, "crate");
130 fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, it: &'tcx hir::Item) {
131 let desc = match it.node {
132 hir::ItemConst(..) => "a constant",
133 hir::ItemEnum(..) => "an enum",
134 hir::ItemFn(..) => "a function",
135 hir::ItemMod(..) => "a module",
136 hir::ItemStatic(..) => "a static",
137 hir::ItemStruct(..) => "a struct",
138 hir::ItemTrait(..) => "a trait",
139 hir::ItemGlobalAsm(..) => "an assembly blob",
140 hir::ItemTy(..) => "a type alias",
141 hir::ItemUnion(..) => "a union",
142 hir::ItemDefaultImpl(..) |
143 hir::ItemExternCrate(..) |
144 hir::ItemForeignMod(..) |
146 hir::ItemUse(..) => return,
149 self.check_missing_docs_attrs(cx, &it.attrs, it.span, desc);
152 fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, trait_item: &'tcx hir::TraitItem) {
153 let desc = match trait_item.node {
154 hir::TraitItemKind::Const(..) => "an associated constant",
155 hir::TraitItemKind::Method(..) => "a trait method",
156 hir::TraitItemKind::Type(..) => "an associated type",
159 self.check_missing_docs_attrs(cx, &trait_item.attrs, trait_item.span, desc);
162 fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, impl_item: &'tcx hir::ImplItem) {
163 // If the method is an impl for a trait, don't doc.
164 let def_id = cx.tcx.hir.local_def_id(impl_item.id);
165 match cx.tcx.associated_item(def_id).container {
166 ty::TraitContainer(_) => return,
167 ty::ImplContainer(cid) => {
168 if cx.tcx.impl_trait_ref(cid).is_some() {
174 let desc = match impl_item.node {
175 hir::ImplItemKind::Const(..) => "an associated constant",
176 hir::ImplItemKind::Method(..) => "a method",
177 hir::ImplItemKind::Type(_) => "an associated type",
179 self.check_missing_docs_attrs(cx, &impl_item.attrs, impl_item.span, desc);
182 fn check_struct_field(&mut self, cx: &LateContext<'a, 'tcx>, sf: &'tcx hir::StructField) {
183 if !sf.is_positional() {
184 self.check_missing_docs_attrs(cx, &sf.attrs, sf.span, "a struct field");
188 fn check_variant(&mut self, cx: &LateContext<'a, 'tcx>, v: &'tcx hir::Variant, _: &hir::Generics) {
189 self.check_missing_docs_attrs(cx, &v.node.attrs, v.span, "a variant");