]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/missing_doc.rs
Merge branch 'macro-use' into HEAD
[rust.git] / clippy_lints / src / missing_doc.rs
index 2f3bdeb053c8fe3406f72939fe5215efbd54e8a9..e921c541be203f59d27197d5d4ba0c5241d9753b 100644 (file)
@@ -1,39 +1,44 @@
-/* This file incorporates work covered by the following copyright and
- * permission notice:
- *   Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
- *   file at the top-level directory of this distribution and at
- *   http://rust-lang.org/COPYRIGHT.
- *
- *   Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
- *   http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
- *   <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
- *   option. This file may not be copied, modified, or distributed
- *   except according to those terms.
- */
-
-/* Note: More specifically this lint is largely inspired (aka copied) from *rustc*'s
- * [`missing_doc`].
- *
- * [`missing_doc`]: https://github.com/rust-lang/rust/blob/d6d05904697d89099b55da3331155392f1db9c00/src/librustc_lint/builtin.rs#L246
- */
+// This file incorporates work covered by the following copyright and
+// permission notice:
+//   Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
+//   file at the top-level directory of this distribution and at
+//   http://rust-lang.org/COPYRIGHT.
+//
+//   Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+//   http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+//   <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+//   option. This file may not be copied, modified, or distributed
+//   except according to those terms.
+//
+
+// Note: More specifically this lint is largely inspired (aka copied) from
+// *rustc*'s
+// [`missing_doc`].
+//
+// [`missing_doc`]: https://github.com/rust-lang/rust/blob/d6d05904697d89099b55da3331155392f1db9c00/src/librustc_lint/builtin.rs#L246
+//
 
 use rustc::hir;
 use rustc::lint::*;
+use rustc::{declare_lint, lint_array};
 use rustc::ty;
 use syntax::ast;
 use syntax::attr;
 use syntax::codemap::Span;
-use utils::in_macro;
+use crate::utils::in_macro;
 
-/// **What it does:** Warns if there is missing doc for any documentable item (public or private).
+/// **What it does:** Warns if there is missing doc for any documentable item
+/// (public or private).
 ///
-/// **Why is this bad?** Doc is good. *rustc* has a `MISSING_DOCS` allowed-by-default lint for
-/// public members, but has no way to enforce documentation of private items. This lint fixes that.
+/// **Why is this bad?** Doc is good. *rustc* has a `MISSING_DOCS`
+/// allowed-by-default lint for
+/// public members, but has no way to enforce documentation of private items.
+/// This lint fixes that.
 ///
 /// **Known problems:** None.
-declare_lint! {
+declare_clippy_lint! {
     pub MISSING_DOCS_IN_PRIVATE_ITEMS,
-    Allow,
+    restriction,
     "detects missing documentation for public and private members"
 }
 
@@ -44,27 +49,25 @@ pub struct MissingDoc {
 }
 
 impl ::std::default::Default for MissingDoc {
-    fn default() -> MissingDoc {
-        MissingDoc::new()
+    fn default() -> Self {
+        Self::new()
     }
 }
 
 impl MissingDoc {
-    pub fn new() -> MissingDoc {
-        MissingDoc {
+    pub fn new() -> Self {
+        Self {
             doc_hidden_stack: vec![false],
         }
     }
 
     fn doc_hidden(&self) -> bool {
-        *self.doc_hidden_stack.last().expect("empty doc_hidden_stack")
+        *self.doc_hidden_stack
+            .last()
+            .expect("empty doc_hidden_stack")
     }
 
-    fn check_missing_docs_attrs(&self,
-                               cx: &LateContext,
-                               attrs: &[ast::Attribute],
-                               sp: Span,
-                               desc: &'static str) {
+    fn check_missing_docs_attrs(&self, cx: &LateContext, attrs: &[ast::Attribute], sp: Span, desc: &'static str) {
         // If we're building a test harness, then warning about
         // documentation is probably not really relevant right now.
         if cx.sess().opts.test {
@@ -76,14 +79,19 @@ fn check_missing_docs_attrs(&self,
             return;
         }
 
-        if in_macro(cx, sp) {
+        if in_macro(sp) {
             return;
         }
 
-        let has_doc = attrs.iter().any(|a| a.is_value_str() && a.name() == "doc");
+        let has_doc = attrs
+            .iter()
+            .any(|a| a.is_value_str() && a.name() == "doc");
         if !has_doc {
-            cx.span_lint(MISSING_DOCS_IN_PRIVATE_ITEMS, sp,
-                         &format!("missing documentation for {}", desc));
+            cx.span_lint(
+                MISSING_DOCS_IN_PRIVATE_ITEMS,
+                sp,
+                &format!("missing documentation for {}", desc),
+            );
         }
     }
 }
@@ -115,20 +123,32 @@ fn check_crate(&mut self, cx: &LateContext<'a, 'tcx>, krate: &'tcx hir::Crate) {
 
     fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, it: &'tcx hir::Item) {
         let desc = match it.node {
-            hir::ItemConst(..) => "a constant",
-            hir::ItemEnum(..) => "an enum",
-            hir::ItemFn(..) => "a function",
-            hir::ItemMod(..) => "a module",
-            hir::ItemStatic(..) => "a static",
-            hir::ItemStruct(..) => "a struct",
-            hir::ItemTrait(..) => "a trait",
-            hir::ItemTy(..) => "a type alias",
-            hir::ItemUnion(..) => "a union",
-            hir::ItemDefaultImpl(..) |
-            hir::ItemExternCrate(..) |
-            hir::ItemForeignMod(..) |
-            hir::ItemImpl(..) |
-            hir::ItemUse(..) => return,
+            hir::ItemKind::Const(..) => "a constant",
+            hir::ItemKind::Enum(..) => "an enum",
+            hir::ItemKind::Fn(..) => {
+                // ignore main()
+                if it.name == "main" {
+                    let def_id = cx.tcx.hir.local_def_id(it.id);
+                    let def_key = cx.tcx.hir.def_key(def_id);
+                    if def_key.parent == Some(hir::def_id::CRATE_DEF_INDEX) {
+                        return;
+                    }
+                }
+                "a function"
+            },
+            hir::ItemKind::Mod(..) => "a module",
+            hir::ItemKind::Static(..) => "a static",
+            hir::ItemKind::Struct(..) => "a struct",
+            hir::ItemKind::Trait(..) => "a trait",
+            hir::ItemKind::TraitAlias(..) => "a trait alias",
+            hir::ItemKind::GlobalAsm(..) => "an assembly blob",
+            hir::ItemKind::Ty(..) => "a type alias",
+            hir::ItemKind::Union(..) => "a union",
+            hir::ItemKind::Existential(..) => "an existential type",
+            hir::ItemKind::ExternCrate(..) |
+            hir::ItemKind::ForeignMod(..) |
+            hir::ItemKind::Impl(..) |
+            hir::ItemKind::Use(..) => return,
         };
 
         self.check_missing_docs_attrs(cx, &it.attrs, it.span, desc);
@@ -136,9 +156,9 @@ fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, it: &'tcx hir::Item) {
 
     fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, trait_item: &'tcx hir::TraitItem) {
         let desc = match trait_item.node {
-            hir::ConstTraitItem(..) => "an associated constant",
-            hir::MethodTraitItem(..) => "a trait method",
-            hir::TypeTraitItem(..) => "an associated type",
+            hir::TraitItemKind::Const(..) => "an associated constant",
+            hir::TraitItemKind::Method(..) => "a trait method",
+            hir::TraitItemKind::Type(..) => "an associated type",
         };
 
         self.check_missing_docs_attrs(cx, &trait_item.attrs, trait_item.span, desc);
@@ -146,20 +166,19 @@ fn check_trait_item(&mut self, cx: &LateContext<'a, 'tcx>, trait_item: &'tcx hir
 
     fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, impl_item: &'tcx hir::ImplItem) {
         // If the method is an impl for a trait, don't doc.
-        let def_id = cx.tcx.map.local_def_id(impl_item.id);
+        let def_id = cx.tcx.hir.local_def_id(impl_item.id);
         match cx.tcx.associated_item(def_id).container {
             ty::TraitContainer(_) => return,
-            ty::ImplContainer(cid) => {
-                if cx.tcx.impl_trait_ref(cid).is_some() {
-                    return
-                }
-            }
+            ty::ImplContainer(cid) => if cx.tcx.impl_trait_ref(cid).is_some() {
+                return;
+            },
         }
 
         let desc = match impl_item.node {
             hir::ImplItemKind::Const(..) => "an associated constant",
             hir::ImplItemKind::Method(..) => "a method",
             hir::ImplItemKind::Type(_) => "an associated type",
+            hir::ImplItemKind::Existential(_) => "an existential type",
         };
         self.check_missing_docs_attrs(cx, &impl_item.attrs, impl_item.span, desc);
     }