]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/enum_glob_use.rs
Merge branch 'macro-use' into HEAD
[rust.git] / clippy_lints / src / enum_glob_use.rs
1 //! lint on `use`ing all variants of an enum
2
3 use rustc::hir::*;
4 use rustc::hir::def::Def;
5 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
6 use rustc::{declare_lint, lint_array};
7 use syntax::ast::NodeId;
8 use syntax::codemap::Span;
9 use crate::utils::span_lint;
10
11 /// **What it does:** Checks for `use Enum::*`.
12 ///
13 /// **Why is this bad?** It is usually better style to use the prefixed name of
14 /// an enumeration variant, rather than importing variants.
15 ///
16 /// **Known problems:** Old-style enumerations that prefix the variants are
17 /// still around.
18 ///
19 /// **Example:**
20 /// ```rust
21 /// use std::cmp::Ordering::*;
22 /// ```
23 declare_clippy_lint! {
24     pub ENUM_GLOB_USE,
25     pedantic,
26     "use items that import all variants of an enum"
27 }
28
29 pub struct EnumGlobUse;
30
31 impl LintPass for EnumGlobUse {
32     fn get_lints(&self) -> LintArray {
33         lint_array!(ENUM_GLOB_USE)
34     }
35 }
36
37 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EnumGlobUse {
38     fn check_mod(&mut self, cx: &LateContext<'a, 'tcx>, m: &'tcx Mod, _: Span, _: NodeId) {
39         // only check top level `use` statements
40         for item in &m.item_ids {
41             self.lint_item(cx, cx.tcx.hir.expect_item(item.id));
42         }
43     }
44 }
45
46 impl EnumGlobUse {
47     fn lint_item(&self, cx: &LateContext, item: &Item) {
48         if item.vis.node.is_pub() {
49             return; // re-exports are fine
50         }
51         if let ItemKind::Use(ref path, UseKind::Glob) = item.node {
52             if let Def::Enum(_) = path.def {
53                 span_lint(
54                     cx,
55                     ENUM_GLOB_USE,
56                     item.span,
57                     "don't use glob imports for enum variants",
58                 );
59             }
60         }
61     }
62 }