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.
11 //! lint on `use`ing all variants of an enum
13 use crate::rustc::hir::*;
14 use crate::rustc::hir::def::Def;
15 use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
16 use crate::rustc::{declare_tool_lint, lint_array};
17 use crate::syntax::ast::NodeId;
18 use crate::syntax::source_map::Span;
19 use crate::utils::span_lint;
21 /// **What it does:** Checks for `use Enum::*`.
23 /// **Why is this bad?** It is usually better style to use the prefixed name of
24 /// an enumeration variant, rather than importing variants.
26 /// **Known problems:** Old-style enumerations that prefix the variants are
31 /// use std::cmp::Ordering::*;
33 declare_clippy_lint! {
36 "use items that import all variants of an enum"
39 pub struct EnumGlobUse;
41 impl LintPass for EnumGlobUse {
42 fn get_lints(&self) -> LintArray {
43 lint_array!(ENUM_GLOB_USE)
47 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EnumGlobUse {
48 fn check_mod(&mut self, cx: &LateContext<'a, 'tcx>, m: &'tcx Mod, _: Span, _: NodeId) {
49 // only check top level `use` statements
50 for item in &m.item_ids {
51 self.lint_item(cx, cx.tcx.hir.expect_item(item.id));
57 fn lint_item(&self, cx: &LateContext<'_, '_>, item: &Item) {
58 if item.vis.node.is_pub() {
59 return; // re-exports are fine
61 if let ItemKind::Use(ref path, UseKind::Glob) = item.node {
62 if let Def::Enum(_) = path.def {
67 "don't use glob imports for enum variants",