rust-clippy/clippy_lints/src/enum_glob_use.rs

51 lines
1.5 KiB
Rust
Raw Normal View History

//! lint on `use`ing all variants of an enum
2018-05-30 08:15:50 +00:00
use crate::utils::span_lint;
2019-12-03 23:16:03 +00:00
use rustc::declare_lint_pass;
use rustc::hir::def::{DefKind, Res};
use rustc::hir::*;
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
2019-12-03 23:16:03 +00:00
use rustc_session::declare_tool_lint;
use syntax::source_map::Span;
2018-03-28 13:24:26 +00:00
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,
2018-03-28 13:24:26 +00:00
pedantic,
"use items that import all variants of an enum"
}
2019-04-08 20:43:55 +00:00
declare_lint_pass!(EnumGlobUse => [ENUM_GLOB_USE]);
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EnumGlobUse {
2019-12-22 14:42:41 +00:00
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
2019-12-22 14:56:34 +00:00
for item in m.item_ids {
2019-10-03 19:09:32 +00:00
lint_item(cx, map.expect_item(item.id));
}
}
}
2019-12-22 14:42:41 +00:00
fn lint_item(cx: &LateContext<'_, '_>, item: &Item<'_>) {
2019-10-03 19:09:32 +00:00
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");
}
}
}