2016-02-03 08:38:23 -06:00
|
|
|
//! lint on `use`ing all variants of an enum
|
|
|
|
|
2018-05-30 03:15:50 -05:00
|
|
|
use crate::utils::span_lint;
|
2018-12-29 09:04:45 -06:00
|
|
|
use rustc::hir::def::Def;
|
|
|
|
use rustc::hir::*;
|
|
|
|
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
|
|
|
|
use rustc::{declare_tool_lint, lint_array};
|
|
|
|
use syntax::ast::NodeId;
|
|
|
|
use syntax::source_map::Span;
|
2016-02-03 08:38:23 -06:00
|
|
|
|
2016-08-06 02:55:04 -05:00
|
|
|
/// **What it does:** Checks for `use Enum::*`.
|
2016-02-03 08:38:23 -06:00
|
|
|
///
|
2016-08-06 02:55:04 -05:00
|
|
|
/// **Why is this bad?** It is usually better style to use the prefixed name of
|
|
|
|
/// an enumeration variant, rather than importing variants.
|
2016-02-03 08:38:23 -06:00
|
|
|
///
|
2016-08-06 02:55:04 -05:00
|
|
|
/// **Known problems:** Old-style enumerations that prefix the variants are
|
2018-03-26 00:48:32 -05:00
|
|
|
/// still around.
|
2016-02-03 08:38:23 -06:00
|
|
|
///
|
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! {
|
2016-08-06 03:18:36 -05:00
|
|
|
pub ENUM_GLOB_USE,
|
2018-03-28 08:24:26 -05:00
|
|
|
pedantic,
|
2016-08-06 03:18:36 -05:00
|
|
|
"use items that import all variants of an enum"
|
|
|
|
}
|
2016-02-03 08:38:23 -06:00
|
|
|
|
|
|
|
pub struct EnumGlobUse;
|
|
|
|
|
|
|
|
impl LintPass for EnumGlobUse {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(ENUM_GLOB_USE)
|
|
|
|
}
|
2019-01-26 13:40:55 -06:00
|
|
|
|
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
"EnumGlobUse"
|
|
|
|
}
|
2016-02-03 08:38:23 -06:00
|
|
|
}
|
|
|
|
|
2016-12-07 06:13:40 -06:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EnumGlobUse {
|
|
|
|
fn check_mod(&mut self, cx: &LateContext<'a, 'tcx>, m: &'tcx Mod, _: Span, _: NodeId) {
|
2016-02-03 08:38:23 -06:00
|
|
|
// only check top level `use` statements
|
|
|
|
for item in &m.item_ids {
|
2018-12-07 18:56:03 -06:00
|
|
|
self.lint_item(cx, cx.tcx.hir().expect_item(item.id));
|
2016-02-03 08:38:23 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EnumGlobUse {
|
2018-07-23 06:01:12 -05:00
|
|
|
fn lint_item(&self, cx: &LateContext<'_, '_>, item: &Item) {
|
2018-07-14 17:00:27 -05:00
|
|
|
if item.vis.node.is_pub() {
|
2016-02-03 08:38:23 -06:00
|
|
|
return; // re-exports are fine
|
|
|
|
}
|
2018-07-16 08:07:39 -05:00
|
|
|
if let ItemKind::Use(ref path, UseKind::Glob) = item.node {
|
2018-03-26 00:48:32 -05:00
|
|
|
if let Def::Enum(_) = path.def {
|
2018-11-27 14:14:15 -06:00
|
|
|
span_lint(cx, ENUM_GLOB_USE, item.span, "don't use glob imports for enum variants");
|
2016-02-03 08:38:23 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|