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