1 //! lint on `use`ing all variants of an enum
3 use crate::utils::span_lint;
4 use rustc::declare_lint_pass;
5 use rustc::hir::def::{DefKind, Res};
7 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
8 use rustc_session::declare_tool_lint;
9 use syntax::source_map::Span;
11 declare_clippy_lint! {
12 /// **What it does:** Checks for `use Enum::*`.
14 /// **Why is this bad?** It is usually better style to use the prefixed name of
15 /// an enumeration variant, rather than importing variants.
17 /// **Known problems:** Old-style enumerations that prefix the variants are
22 /// use std::cmp::Ordering::*;
26 "use items that import all variants of an enum"
29 declare_lint_pass!(EnumGlobUse => [ENUM_GLOB_USE]);
31 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EnumGlobUse {
32 fn check_mod(&mut self, cx: &LateContext<'a, 'tcx>, m: &'tcx Mod, _: Span, _: HirId) {
33 let map = cx.tcx.hir();
34 // only check top level `use` statements
35 for item in &m.item_ids {
36 lint_item(cx, map.expect_item(item.id));
41 fn lint_item(cx: &LateContext<'_, '_>, item: &Item) {
42 if item.vis.node.is_pub() {
43 return; // re-exports are fine
45 if let ItemKind::Use(ref path, UseKind::Glob) = item.kind {
46 if let Res::Def(DefKind::Enum, _) = path.res {
47 span_lint(cx, ENUM_GLOB_USE, item.span, "don't use glob imports for enum variants");