]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/enum_glob_use.rs
`enum glob use` and `copies` left
[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::{LateLintPass, LintPass, LateContext, LintArray};
6 use syntax::ast::NodeId;
7 use syntax::codemap::Span;
8 use 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_lint! {
23     pub ENUM_GLOB_USE,
24     Allow,
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 LateLintPass for EnumGlobUse {
37     fn check_mod(&mut self, cx: &LateContext, m: &Mod, _: Span, _: NodeId) {
38         // only check top level `use` statements
39         for item in &m.item_ids {
40             self.lint_item(cx, cx.krate.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             // FIXME: ask jseyfried why the qpath.def for `use std::cmp::Ordering::*;`
52             // extracted through `ItemUse(ref qpath, UseKind::Glob)` is a `Mod` and not an `Enum`
53             if let Def::Enum(_) = path.def {
54                 span_lint(cx, ENUM_GLOB_USE, item.span, "don't use glob imports for enum variants");
55             }
56         }
57     }
58 }