]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/missing_doc.rs
Add a test for `doc(include)`
[rust.git] / clippy_lints / src / missing_doc.rs
index 750fdfde50629064cb0d91e78849e7282e005cdd..f5dab6d1feda73abebd17319c5d634f6b8f36c50 100644 (file)
@@ -1,16 +1,3 @@
-// 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 crate::utils::{in_macro, span_lint};
+use if_chain::if_chain;
 use rustc::hir;
-use rustc::lint::*;
-use rustc::{declare_lint, lint_array};
+use rustc::lint::{LateContext, LateLintPass, LintArray, LintContext, LintPass};
 use rustc::ty;
-use syntax::ast;
+use rustc::{declare_tool_lint, lint_array};
+use syntax::ast::{self, MetaItem, MetaItemKind};
 use syntax::attr;
-use syntax::codemap::Span;
-use crate::utils::in_macro;
-
-/// **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.
-///
-/// **Known problems:** None.
+use syntax::source_map::Span;
+
 declare_clippy_lint! {
+    /// **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.
+    ///
+    /// **Known problems:** None.
     pub MISSING_DOCS_IN_PRIVATE_ITEMS,
     restriction,
     "detects missing documentation for public and private members"
@@ -62,12 +50,30 @@ pub fn new() -> Self {
     }
 
     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 has_include(meta: Option<MetaItem>) -> bool {
+        if_chain! {
+            if let Some(meta) = meta;
+            if let MetaItemKind::List(list) = meta.node;
+            if let Some(meta) = list.get(0);
+            if let Some(name) = meta.name();
+            then {
+                name == "include"
+            } else {
+                false
+            }
+        }
+    }
+
+    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 {
@@ -85,9 +91,10 @@ fn check_missing_docs_attrs(&self, cx: &LateContext, attrs: &[ast::Attribute], s
 
         let has_doc = attrs
             .iter()
-            .any(|a| a.is_value_str() && a.name() == "doc");
+            .any(|a| a.name() == "doc" && (a.is_value_str() || Self::has_include(a.meta())));
         if !has_doc {
-            cx.span_lint(
+            span_lint(
+                cx,
                 MISSING_DOCS_IN_PRIVATE_ITEMS,
                 sp,
                 &format!("missing documentation for {}", desc),
@@ -100,16 +107,22 @@ impl LintPass for MissingDoc {
     fn get_lints(&self) -> LintArray {
         lint_array![MISSING_DOCS_IN_PRIVATE_ITEMS]
     }
+
+    fn name(&self) -> &'static str {
+        "MissingDoc"
+    }
 }
 
 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MissingDoc {
     fn enter_lint_attrs(&mut self, _: &LateContext<'a, 'tcx>, attrs: &'tcx [ast::Attribute]) {
-        let doc_hidden = self.doc_hidden() || attrs.iter().any(|attr| {
-            attr.check_name("doc") && match attr.meta_item_list() {
-                None => false,
-                Some(l) => attr::list_contains_name(&l[..], "hidden"),
-            }
-        });
+        let doc_hidden = self.doc_hidden()
+            || attrs.iter().any(|attr| {
+                attr.check_name("doc")
+                    && match attr.meta_item_list() {
+                        None => false,
+                        Some(l) => attr::list_contains_name(&l[..], "hidden"),
+                    }
+            });
         self.doc_hidden_stack.push(doc_hidden);
     }
 
@@ -127,9 +140,9 @@ fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, it: &'tcx hir::Item) {
             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 it.ident.name == "main" {
+                    let def_id = cx.tcx.hir().local_def_id_from_hir_id(it.hir_id);
+                    let def_key = cx.tcx.hir().def_key(def_id);
                     if def_key.parent == Some(hir::def_id::CRATE_DEF_INDEX) {
                         return;
                     }
@@ -141,14 +154,14 @@ fn check_item(&mut self, cx: &LateContext<'a, 'tcx>, it: &'tcx hir::Item) {
             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,
+            hir::ItemKind::ExternCrate(..)
+            | hir::ItemKind::ForeignMod(..)
+            | hir::ItemKind::GlobalAsm(..)
+            | hir::ItemKind::Impl(..)
+            | hir::ItemKind::Use(..) => return,
         };
 
         self.check_missing_docs_attrs(cx, &it.attrs, it.span, desc);
@@ -166,11 +179,13 @@ 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.hir.local_def_id(impl_item.id);
+        let def_id = cx.tcx.hir().local_def_id_from_hir_id(impl_item.hir_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;
+                }
             },
         }
 
@@ -178,6 +193,7 @@ fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, impl_item: &'tcx hir::
             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);
     }