1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
10 //! lint on `use`ing all variants of an enum
12 use crate::rustc::hir::def::Def;
13 use crate::rustc::hir::*;
14 use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
15 use crate::rustc::{declare_tool_lint, lint_array};
16 use crate::syntax::ast::NodeId;
17 use crate::syntax::source_map::Span;
18 use crate::utils::span_lint;
20 /// **What it does:** Checks for `use Enum::*`.
22 /// **Why is this bad?** It is usually better style to use the prefixed name of
23 /// an enumeration variant, rather than importing variants.
25 /// **Known problems:** Old-style enumerations that prefix the variants are
30 /// use std::cmp::Ordering::*;
32 declare_clippy_lint! {
35 "use items that import all variants of an enum"
38 pub struct EnumGlobUse;
40 impl LintPass for EnumGlobUse {
41 fn get_lints(&self) -> LintArray {
42 lint_array!(ENUM_GLOB_USE)
46 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EnumGlobUse {
47 fn check_mod(&mut self, cx: &LateContext<'a, 'tcx>, m: &'tcx Mod, _: Span, _: NodeId) {
48 // only check top level `use` statements
49 for item in &m.item_ids {
50 self.lint_item(cx, cx.tcx.hir().expect_item(item.id));
56 fn lint_item(&self, cx: &LateContext<'_, '_>, item: &Item) {
57 if item.vis.node.is_pub() {
58 return; // re-exports are fine
60 if let ItemKind::Use(ref path, UseKind::Glob) = item.node {
61 if let Def::Enum(_) = path.def {
62 span_lint(cx, ENUM_GLOB_USE, item.span, "don't use glob imports for enum variants");