rust-clippy/clippy_lints/src/enum_glob_use.rs
2019-12-22 15:56:34 +01:00

50 lines
1.5 KiB
Rust

//! lint on `use`ing all variants of an enum
use crate::utils::span_lint;
use rustc::declare_lint_pass;
use rustc::hir::def::{DefKind, Res};
use rustc::hir::*;
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
use rustc_session::declare_tool_lint;
use syntax::source_map::Span;
declare_clippy_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.
///
/// **Example:**
/// ```rust
/// use std::cmp::Ordering::*;
/// ```
pub ENUM_GLOB_USE,
pedantic,
"use items that import all variants of an enum"
}
declare_lint_pass!(EnumGlobUse => [ENUM_GLOB_USE]);
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EnumGlobUse {
fn check_mod(&mut self, cx: &LateContext<'a, 'tcx>, m: &'tcx Mod<'_>, _: Span, _: HirId) {
let map = cx.tcx.hir();
// only check top level `use` statements
for item in m.item_ids {
lint_item(cx, map.expect_item(item.id));
}
}
}
fn lint_item(cx: &LateContext<'_, '_>, item: &Item<'_>) {
if item.vis.node.is_pub() {
return; // re-exports are fine
}
if let ItemKind::Use(ref path, UseKind::Glob) = item.kind {
if let Res::Def(DefKind::Enum, _) = path.res {
span_lint(cx, ENUM_GLOB_USE, item.span, "don't use glob imports for enum variants");
}
}
}