]> git.proxmox.com Git - rustc.git/blob - src/tools/clippy/clippy_lints/src/missing_doc.rs
New upstream version 1.23.0+dfsg1
[rustc.git] / src / tools / clippy / clippy_lints / src / missing_doc.rs
1 // This file incorporates work covered by the following copyright and
2 // permission notice:
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.
6 //
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.
12 //
13
14 // Note: More specifically this lint is largely inspired (aka copied) from
15 // *rustc*'s
16 // [`missing_doc`].
17 //
18 // [`missing_doc`]: https://github.com/rust-lang/rust/blob/d6d05904697d89099b55da3331155392f1db9c00/src/librustc_lint/builtin.rs#L246
19 //
20
21 use rustc::hir;
22 use rustc::lint::*;
23 use rustc::ty;
24 use syntax::ast;
25 use syntax::attr;
26 use syntax::codemap::Span;
27 use utils::in_macro;
28
29 /// **What it does:** Warns if there is missing doc for any documentable item
30 /// (public or private).
31 ///
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.
36 ///
37 /// **Known problems:** None.
38 declare_lint! {
39 pub MISSING_DOCS_IN_PRIVATE_ITEMS,
40 Allow,
41 "detects missing documentation for public and private members"
42 }
43
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>,
48 }
49
50 impl ::std::default::Default for MissingDoc {
51 fn default() -> Self {
52 Self::new()
53 }
54 }
55
56 impl MissingDoc {
57 pub fn new() -> Self {
58 Self {
59 doc_hidden_stack: vec![false],
60 }
61 }
62
63 fn doc_hidden(&self) -> bool {
64 *self.doc_hidden_stack
65 .last()
66 .expect("empty doc_hidden_stack")
67 }
68
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 {
73 return;
74 }
75
76 // `#[doc(hidden)]` disables missing_docs check.
77 if self.doc_hidden() {
78 return;
79 }
80
81 if in_macro(sp) {
82 return;
83 }
84
85 let has_doc = attrs
86 .iter()
87 .any(|a| a.is_value_str() && a.name().map_or(false, |n| n == "doc"));
88 if !has_doc {
89 cx.span_lint(
90 MISSING_DOCS_IN_PRIVATE_ITEMS,
91 sp,
92 &format!("missing documentation for {}", desc),
93 );
94 }
95 }
96 }
97
98 impl LintPass for MissingDoc {
99 fn get_lints(&self) -> LintArray {
100 lint_array![MISSING_DOCS_IN_PRIVATE_ITEMS]
101 }
102 }
103
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() {
108 None => false,
109 Some(l) => attr::list_contains_name(&l[..], "hidden"),
110 }
111 });
112 self.doc_hidden_stack.push(doc_hidden);
113 }
114
115 fn exit_lint_attrs(&mut self, _: &LateContext<'a, 'tcx>, _: &'tcx [ast::Attribute]) {
116 self.doc_hidden_stack.pop().expect("empty doc_hidden_stack");
117 }
118
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");
121 }
122
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",
127 hir::ItemFn(..) => "a function",
128 hir::ItemMod(..) => "a module",
129 hir::ItemStatic(..) => "a static",
130 hir::ItemStruct(..) => "a struct",
131 hir::ItemTrait(..) => "a trait",
132 hir::ItemGlobalAsm(..) => "an assembly blob",
133 hir::ItemTy(..) => "a type alias",
134 hir::ItemUnion(..) => "a union",
135 hir::ItemAutoImpl(..) |
136 hir::ItemExternCrate(..) |
137 hir::ItemForeignMod(..) |
138 hir::ItemImpl(..) |
139 hir::ItemUse(..) => return,
140 };
141
142 self.check_missing_docs_attrs(cx, &it.attrs, it.span, desc);
143 }
144
145 fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, trait_item: &'tcx hir::TraitItem) {
146 let desc = match trait_item.node {
147 hir::TraitItemKind::Const(..) => "an associated constant",
148 hir::TraitItemKind::Method(..) => "a trait method",
149 hir::TraitItemKind::Type(..) => "an associated type",
150 };
151
152 self.check_missing_docs_attrs(cx, &trait_item.attrs, trait_item.span, desc);
153 }
154
155 fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, impl_item: &'tcx hir::ImplItem) {
156 // If the method is an impl for a trait, don't doc.
157 let def_id = cx.tcx.hir.local_def_id(impl_item.id);
158 match cx.tcx.associated_item(def_id).container {
159 ty::TraitContainer(_) => return,
160 ty::ImplContainer(cid) => if cx.tcx.impl_trait_ref(cid).is_some() {
161 return;
162 },
163 }
164
165 let desc = match impl_item.node {
166 hir::ImplItemKind::Const(..) => "an associated constant",
167 hir::ImplItemKind::Method(..) => "a method",
168 hir::ImplItemKind::Type(_) => "an associated type",
169 };
170 self.check_missing_docs_attrs(cx, &impl_item.attrs, impl_item.span, desc);
171 }
172
173 fn check_struct_field(&mut self, cx: &LateContext<'a, 'tcx>, sf: &'tcx hir::StructField) {
174 if !sf.is_positional() {
175 self.check_missing_docs_attrs(cx, &sf.attrs, sf.span, "a struct field");
176 }
177 }
178
179 fn check_variant(&mut self, cx: &LateContext<'a, 'tcx>, v: &'tcx hir::Variant, _: &hir::Generics) {
180 self.check_missing_docs_attrs(cx, &v.node.attrs, v.span, "a variant");
181 }
182 }