rust/clippy_lints/src/enum_glob_use.rs

63 lines
1.7 KiB
Rust
Raw Normal View History

//! lint on `use`ing all variants of an enum
use rustc::hir::*;
use rustc::hir::def::Def;
2017-09-05 04:33:04 -05:00
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
2018-07-19 02:53:23 -05:00
use rustc::{declare_lint, lint_array};
use syntax::ast::NodeId;
2016-02-24 10:38:57 -06:00
use syntax::codemap::Span;
2018-05-30 03:15:50 -05:00
use crate::utils::span_lint;
/// **What it does:** Checks for `use Enum::*`.
///
/// **Why is this bad?** It is usually better style to use the prefixed name of
/// an enumeration variant, rather than importing variants.
///
/// **Known problems:** Old-style enumerations that prefix the variants are
/// still around.
///
2016-07-15 17:25:44 -05:00
/// **Example:**
/// ```rust
/// use std::cmp::Ordering::*;
/// ```
2018-03-28 08:24:26 -05:00
declare_clippy_lint! {
pub ENUM_GLOB_USE,
2018-03-28 08:24:26 -05:00
pedantic,
"use items that import all variants of an enum"
}
pub struct EnumGlobUse;
impl LintPass for EnumGlobUse {
fn get_lints(&self) -> LintArray {
lint_array!(ENUM_GLOB_USE)
}
}
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EnumGlobUse {
fn check_mod(&mut self, cx: &LateContext<'a, 'tcx>, m: &'tcx Mod, _: Span, _: NodeId) {
// only check top level `use` statements
for item in &m.item_ids {
self.lint_item(cx, cx.tcx.hir.expect_item(item.id));
}
}
}
impl EnumGlobUse {
2018-07-23 06:01:12 -05:00
fn lint_item(&self, cx: &LateContext<'_, '_>, item: &Item) {
if item.vis.node.is_pub() {
return; // re-exports are fine
}
2018-07-16 08:07:39 -05:00
if let ItemKind::Use(ref path, UseKind::Glob) = item.node {
if let Def::Enum(_) = path.def {
span_lint(
cx,
ENUM_GLOB_USE,
item.span,
"don't use glob imports for enum variants",
);
}
}
}
}