2016-02-03 14:38:23 +00:00
|
|
|
//! lint on `use`ing all variants of an enum
|
|
|
|
|
2016-04-07 15:46:48 +00:00
|
|
|
use rustc::hir::*;
|
2017-09-05 09:33:04 +00:00
|
|
|
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
|
2016-02-03 14:38:23 +00:00
|
|
|
use syntax::ast::NodeId;
|
2016-02-24 16:38:57 +00:00
|
|
|
use syntax::codemap::Span;
|
|
|
|
use utils::span_lint;
|
2016-02-03 14:38:23 +00:00
|
|
|
|
2016-08-06 07:55:04 +00:00
|
|
|
/// **What it does:** Checks for `use Enum::*`.
|
2016-02-03 14:38:23 +00:00
|
|
|
///
|
2016-08-06 07:55:04 +00: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 14:38:23 +00:00
|
|
|
///
|
2016-08-06 07:55:04 +00:00
|
|
|
/// **Known problems:** Old-style enumerations that prefix the variants are
|
2018-01-25 07:25:58 +00:00
|
|
|
/// still around. May cause problems with modules that are not snake_case (see
|
|
|
|
/// [#2397](https://github.com/rust-lang-nursery/rust-clippy/issues/2397))
|
2016-02-03 14:38:23 +00:00
|
|
|
///
|
2016-07-15 22:25:44 +00:00
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// use std::cmp::Ordering::*;
|
|
|
|
/// ```
|
2016-08-06 08:18:36 +00:00
|
|
|
declare_lint! {
|
|
|
|
pub ENUM_GLOB_USE,
|
|
|
|
Allow,
|
|
|
|
"use items that import all variants of an enum"
|
|
|
|
}
|
2016-02-03 14:38:23 +00:00
|
|
|
|
|
|
|
pub struct EnumGlobUse;
|
|
|
|
|
|
|
|
impl LintPass for EnumGlobUse {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(ENUM_GLOB_USE)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 12:13:40 +00: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 14:38:23 +00:00
|
|
|
// only check top level `use` statements
|
|
|
|
for item in &m.item_ids {
|
2017-06-11 02:34:47 +00:00
|
|
|
self.lint_item(cx, cx.tcx.hir.expect_item(item.id));
|
2016-02-03 14:38:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EnumGlobUse {
|
|
|
|
fn lint_item(&self, cx: &LateContext, item: &Item) {
|
|
|
|
if item.vis == Visibility::Public {
|
|
|
|
return; // re-exports are fine
|
|
|
|
}
|
2016-12-01 21:31:56 +00:00
|
|
|
if let ItemUse(ref path, UseKind::Glob) = item.node {
|
2016-12-02 16:38:31 +00:00
|
|
|
// FIXME: ask jseyfried why the qpath.def for `use std::cmp::Ordering::*;`
|
2017-08-09 07:30:56 +00:00
|
|
|
// extracted through `ItemUse(ref qpath, UseKind::Glob)` is a `Mod` and not an
|
|
|
|
// `Enum`
|
2016-12-20 17:21:30 +00:00
|
|
|
// if let Def::Enum(_) = path.def {
|
2017-08-09 07:30:56 +00:00
|
|
|
if path.segments
|
|
|
|
.last()
|
|
|
|
.and_then(|seg| seg.name.as_str().chars().next())
|
|
|
|
.map_or(false, char::is_uppercase)
|
|
|
|
{
|
2016-12-01 21:31:56 +00:00
|
|
|
span_lint(cx, ENUM_GLOB_USE, item.span, "don't use glob imports for enum variants");
|
2016-02-03 14:38:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|