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