Fix enum_glob_use false positives

Closes #2397.

This checks the def of the `ItemUse` path instead of checking the
capitalization of the path segements. It was noted that this def would
sometimes be `Def::Mod` instead of `Def::Enum` but it seems correct now.
This commit is contained in:
Michael Wright 2018-03-26 07:48:32 +02:00
parent b01b0083ba
commit 546d2fec29
2 changed files with 15 additions and 12 deletions

View file

@ -1,6 +1,7 @@
//! lint on `use`ing all variants of an enum
use rustc::hir::*;
use rustc::hir::def::Def;
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
use syntax::ast::NodeId;
use syntax::codemap::Span;
@ -12,8 +13,7 @@ use utils::span_lint;
/// an enumeration variant, rather than importing variants.
///
/// **Known problems:** Old-style enumerations that prefix the variants are
/// still around. May cause problems with modules that are not snake_case (see
/// [#2397](https://github.com/rust-lang-nursery/rust-clippy/issues/2397))
/// still around.
///
/// **Example:**
/// ```rust
@ -48,16 +48,13 @@ impl EnumGlobUse {
return; // re-exports are fine
}
if let ItemUse(ref path, UseKind::Glob) = item.node {
// FIXME: ask jseyfried why the qpath.def for `use std::cmp::Ordering::*;`
// extracted through `ItemUse(ref qpath, UseKind::Glob)` is a `Mod` and not an
// `Enum`
// if let Def::Enum(_) = path.def {
if path.segments
.last()
.and_then(|seg| seg.name.as_str().chars().next())
.map_or(false, char::is_uppercase)
{
span_lint(cx, ENUM_GLOB_USE, item.span, "don't use glob imports for enum variants");
if let Def::Enum(_) = path.def {
span_lint(
cx,
ENUM_GLOB_USE,
item.span,
"don't use glob imports for enum variants",
);
}
}
}

View file

@ -23,4 +23,10 @@ mod tests {
use super::*;
}
#[allow(non_snake_case)]
mod CamelCaseName {
}
use CamelCaseName::*;
fn main() {}