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