]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/enum_glob_use.rs
Merge branch 'macro-use' into HEAD
[rust.git] / clippy_lints / src / enum_glob_use.rs
index 1bdaf7b867fd92b8949448e39c332b328f0cd001..10cf497725c5306efbe1d152198e84cfe60aeb37 100644 (file)
@@ -2,25 +2,29 @@
 
 use rustc::hir::*;
 use rustc::hir::def::Def;
-use rustc::hir::map::Node::NodeItem;
-use rustc::lint::{LateLintPass, LintPass, LateContext, LintArray, LintContext};
-use rustc::middle::cstore::DefLike;
+use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
+use rustc::{declare_lint, lint_array};
 use syntax::ast::NodeId;
 use syntax::codemap::Span;
-use utils::span_lint;
+use crate::utils::span_lint;
 
-/// **What it does:** Warns when `use`ing all variants of an enumeration.
+/// **What it does:** Checks for `use Enum::*`.
 ///
-/// **Why is this bad?** It is usually better style to use the prefixed name of an enumeration variant, rather than importing variants
+/// **Why is this bad?** It is usually better style to use the prefixed name of
+/// an enumeration variant, rather than importing variants.
 ///
-/// **Known problems:** Old-style enumerations that prefix the variants are still around
+/// **Known problems:** Old-style enumerations that prefix the variants are
+/// still around.
 ///
 /// **Example:**
 /// ```rust
 /// use std::cmp::Ordering::*;
 /// ```
-declare_lint! { pub ENUM_GLOB_USE, Allow,
-    "finds use items that import all variants of an enum" }
+declare_clippy_lint! {
+    pub ENUM_GLOB_USE,
+    pedantic,
+    "use items that import all variants of an enum"
+}
 
 pub struct EnumGlobUse;
 
@@ -30,38 +34,28 @@ fn get_lints(&self) -> LintArray {
     }
 }
 
-impl LateLintPass for EnumGlobUse {
-    fn check_mod(&mut self, cx: &LateContext, m: &Mod, _: Span, _: NodeId) {
+impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EnumGlobUse {
+    fn check_mod(&mut self, cx: &LateContext<'a, 'tcx>, m: &'tcx Mod, _: Span, _: NodeId) {
         // only check top level `use` statements
         for item in &m.item_ids {
-            self.lint_item(cx, cx.krate.item(item.id));
+            self.lint_item(cx, cx.tcx.hir.expect_item(item.id));
         }
     }
 }
 
 impl EnumGlobUse {
     fn lint_item(&self, cx: &LateContext, item: &Item) {
-        if item.vis == Visibility::Public {
+        if item.vis.node.is_pub() {
             return; // re-exports are fine
         }
-        if let ItemUse(ref item_use) = item.node {
-            if let ViewPath_::ViewPathGlob(_) = item_use.node {
-                if let Some(def) = cx.tcx.def_map.borrow().get(&item.id) {
-                    if let Some(node_id) = cx.tcx.map.as_local_node_id(def.full_def().def_id()) {
-                        if let Some(NodeItem(it)) = cx.tcx.map.find(node_id) {
-                            if let ItemEnum(..) = it.node {
-                                span_lint(cx, ENUM_GLOB_USE, item.span, "don't use glob imports for enum variants");
-                            }
-                        }
-                    } else {
-                        let child = cx.sess().cstore.item_children(def.full_def().def_id());
-                        if let Some(child) = child.first() {
-                            if let DefLike::DlDef(Def::Variant(..)) = child.def {
-                                span_lint(cx, ENUM_GLOB_USE, item.span, "don't use glob imports for enum variants");
-                            }
-                        }
-                    }
-                }
+        if let ItemKind::Use(ref path, UseKind::Glob) = item.node {
+            if let Def::Enum(_) = path.def {
+                span_lint(
+                    cx,
+                    ENUM_GLOB_USE,
+                    item.span,
+                    "don't use glob imports for enum variants",
+                );
             }
         }
     }