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.
34 use syntax::codemap::Span;
37 /// **What it does:** Warns if there is missing doc for any documentable item
38 /// (public or private).
40 /// **Why is this bad?** Doc is good. *rustc* has a `MISSING_DOCS`
41 /// allowed-by-default lint for
42 /// public members, but has no way to enforce documentation of private items.
43 /// This lint fixes that.
45 /// **Known problems:** None.
47 pub MISSING_DOCS_IN_PRIVATE_ITEMS,
49 "detects missing documentation for public and private members"
52 pub struct MissingDoc {
53 /// Stack of whether #[doc(hidden)] is set
54 /// at each level which has lint attributes.
55 doc_hidden_stack: Vec<bool>,
58 impl ::std::default::Default for MissingDoc {
59 fn default() -> MissingDoc {
65 pub fn new() -> MissingDoc {
66 MissingDoc { doc_hidden_stack: vec![false] }
69 fn doc_hidden(&self) -> bool {
70 *self.doc_hidden_stack.last().expect(
71 "empty doc_hidden_stack",
75 fn check_missing_docs_attrs(&self, cx: &LateContext, attrs: &[ast::Attribute], sp: Span, desc: &'static str) {
76 // If we're building a test harness, then warning about
77 // documentation is probably not really relevant right now.
78 if cx.sess().opts.test {
82 // `#[doc(hidden)]` disables missing_docs check.
83 if self.doc_hidden() {
91 let has_doc = attrs.iter().any(|a| {
92 a.is_value_str() && a.name().map_or(false, |n| n == "doc")
96 MISSING_DOCS_IN_PRIVATE_ITEMS,
98 &format!("missing documentation for {}", desc),
104 impl LintPass for MissingDoc {
105 fn get_lints(&self) -> LintArray {
106 lint_array![MISSING_DOCS_IN_PRIVATE_ITEMS]
110 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDoc {
111 fn enter_lint_attrs(&mut self, _: &LateContext<'a, 'tcx>, attrs: &'tcx [ast::Attribute]) {
112 let doc_hidden = self.doc_hidden() ||
113 attrs.iter().any(|attr| {
114 attr.check_name("doc") &&
115 match attr.meta_item_list() {
117 Some(l) => attr::list_contains_name(&l[..], "hidden"),
120 self.doc_hidden_stack.push(doc_hidden);
123 fn exit_lint_attrs(&mut self, _: &LateContext<'a, 'tcx>, _: &'tcx [ast::Attribute]) {
124 self.doc_hidden_stack.pop().expect("empty doc_hidden_stack");
127 fn check_crate(&mut self, cx: &LateContext<'a, 'tcx>, krate: &'tcx hir::Crate) {
128 self.check_missing_docs_attrs(cx, &krate.attrs, krate.span, "crate");
131 fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, it: &'tcx hir::Item) {
132 let desc = match it.node {
133 hir::ItemConst(..) => "a constant",
134 hir::ItemEnum(..) => "an enum",
135 hir::ItemFn(..) => "a function",
136 hir::ItemMod(..) => "a module",
137 hir::ItemStatic(..) => "a static",
138 hir::ItemStruct(..) => "a struct",
139 hir::ItemTrait(..) => "a trait",
140 hir::ItemGlobalAsm(..) => "an assembly blob",
141 hir::ItemTy(..) => "a type alias",
142 hir::ItemUnion(..) => "a union",
143 hir::ItemDefaultImpl(..) |
144 hir::ItemExternCrate(..) |
145 hir::ItemForeignMod(..) |
147 hir::ItemUse(..) => return,
150 self.check_missing_docs_attrs(cx, &it.attrs, it.span, desc);
153 fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, trait_item: &'tcx hir::TraitItem) {
154 let desc = match trait_item.node {
155 hir::TraitItemKind::Const(..) => "an associated constant",
156 hir::TraitItemKind::Method(..) => "a trait method",
157 hir::TraitItemKind::Type(..) => "an associated type",
160 self.check_missing_docs_attrs(cx, &trait_item.attrs, trait_item.span, desc);
163 fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, impl_item: &'tcx hir::ImplItem) {
164 // If the method is an impl for a trait, don't doc.
165 let def_id = cx.tcx.hir.local_def_id(impl_item.id);
166 match cx.tcx.associated_item(def_id).container {
167 ty::TraitContainer(_) => return,
168 ty::ImplContainer(cid) => {
169 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");