]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/enum_glob_use.rs
Auto merge of #3845 - euclio:unused-comments, r=phansch
[rust.git] / clippy_lints / src / enum_glob_use.rs
1 //! lint on `use`ing all variants of an enum
2
3 use crate::utils::span_lint;
4 use rustc::hir::def::Def;
5 use rustc::hir::*;
6 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
7 use rustc::{declare_tool_lint, lint_array};
8 use syntax::source_map::Span;
9
10 declare_clippy_lint! {
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     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     fn name(&self) -> &'static str {
36         "EnumGlobUse"
37     }
38 }
39
40 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EnumGlobUse {
41     fn check_mod(&mut self, cx: &LateContext<'a, 'tcx>, m: &'tcx Mod, _: Span, _: HirId) {
42         // only check top level `use` statements
43         for item in &m.item_ids {
44             self.lint_item(cx, cx.tcx.hir().expect_item(item.id));
45         }
46     }
47 }
48
49 impl EnumGlobUse {
50     fn lint_item(&self, cx: &LateContext<'_, '_>, item: &Item) {
51         if item.vis.node.is_pub() {
52             return; // re-exports are fine
53         }
54         if let ItemKind::Use(ref path, UseKind::Glob) = item.node {
55             if let Def::Enum(_) = path.def {
56                 span_lint(cx, ENUM_GLOB_USE, item.span, "don't use glob imports for enum variants");
57             }
58         }
59     }
60 }