2016-02-03 14:38:23 +00:00
|
|
|
//! 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;
|
2019-05-04 00:03:12 +00:00
|
|
|
use rustc::hir::def::{DefKind, Res};
|
2018-12-29 15:04:45 +00:00
|
|
|
use rustc::hir::*;
|
|
|
|
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
|
2019-12-03 23:16:03 +00:00
|
|
|
use rustc_session::declare_tool_lint;
|
2018-12-29 15:04:45 +00:00
|
|
|
use syntax::source_map::Span;
|
2016-02-03 14:38:23 +00:00
|
|
|
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2019-03-05 16:50:33 +00:00
|
|
|
/// **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::*;
|
|
|
|
/// ```
|
2016-08-06 08:18:36 +00:00
|
|
|
pub ENUM_GLOB_USE,
|
2018-03-28 13:24:26 +00:00
|
|
|
pedantic,
|
2016-08-06 08:18:36 +00:00
|
|
|
"use items that import all variants of an enum"
|
|
|
|
}
|
2016-02-03 14:38:23 +00:00
|
|
|
|
2019-04-08 20:43:55 +00:00
|
|
|
declare_lint_pass!(EnumGlobUse => [ENUM_GLOB_USE]);
|
2016-02-03 14:38:23 +00:00
|
|
|
|
2016-12-07 12:13:40 +00:00
|
|
|
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) {
|
2019-03-29 04:47:09 +00:00
|
|
|
let map = cx.tcx.hir();
|
2016-02-03 14:38:23 +00:00
|
|
|
// 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));
|
2016-02-03 14:38:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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");
|
2016-02-03 14:38:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|