Move enum_glob_use lint into wildcard_imports pass

This commit is contained in:
flip1995 2020-01-14 13:52:08 +01:00
parent 3f5ed28524
commit 06a6189376
No known key found for this signature in database
GPG Key ID: 693086869D506637
7 changed files with 92 additions and 79 deletions

View File

@ -1,49 +0,0 @@
//! lint on `use`ing all variants of an enum
use crate::utils::span_lint;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::*;
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::source_map::Span;
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,
pedantic,
"use items that import all variants of an enum"
}
declare_lint_pass!(EnumGlobUse => [ENUM_GLOB_USE]);
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EnumGlobUse {
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
for item in m.item_ids {
lint_item(cx, map.expect_item(item.id));
}
}
}
fn lint_item(cx: &LateContext<'_, '_>, item: &Item<'_>) {
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");
}
}
}

View File

@ -197,7 +197,6 @@ macro_rules! declare_clippy_lint {
pub mod empty_enum;
pub mod entry;
pub mod enum_clike;
pub mod enum_glob_use;
pub mod enum_variants;
pub mod eq_op;
pub mod erasing_op;
@ -520,7 +519,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
&empty_enum::EMPTY_ENUM,
&entry::MAP_ENTRY,
&enum_clike::ENUM_CLIKE_UNPORTABLE_VARIANT,
&enum_glob_use::ENUM_GLOB_USE,
&enum_variants::ENUM_VARIANT_NAMES,
&enum_variants::MODULE_INCEPTION,
&enum_variants::MODULE_NAME_REPETITIONS,
@ -814,6 +812,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
&use_self::USE_SELF,
&vec::USELESS_VEC,
&wildcard_dependencies::WILDCARD_DEPENDENCIES,
&wildcard_imports::ENUM_GLOB_USE,
&wildcard_imports::WILDCARD_IMPORTS,
&write::PRINTLN_EMPTY_STRING,
&write::PRINT_LITERAL,
@ -837,7 +836,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
store.register_late_pass(move || box types::Types::new(vec_box_size_threshold));
store.register_late_pass(|| box booleans::NonminimalBool);
store.register_late_pass(|| box eq_op::EqOp);
store.register_late_pass(|| box enum_glob_use::EnumGlobUse);
store.register_late_pass(|| box enum_clike::UnportableVariant);
store.register_late_pass(|| box float_literal::FloatLiteral);
let verbose_bit_mask_threshold = conf.verbose_bit_mask_threshold;
@ -1064,7 +1062,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
LintId::of(&doc::DOC_MARKDOWN),
LintId::of(&doc::MISSING_ERRORS_DOC),
LintId::of(&empty_enum::EMPTY_ENUM),
LintId::of(&enum_glob_use::ENUM_GLOB_USE),
LintId::of(&enum_variants::MODULE_NAME_REPETITIONS),
LintId::of(&enum_variants::PUB_ENUM_VARIANT_NAMES),
LintId::of(&eta_reduction::REDUNDANT_CLOSURE_FOR_METHOD_CALLS),
@ -1108,6 +1105,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
LintId::of(&unicode::NON_ASCII_LITERAL),
LintId::of(&unicode::UNICODE_NOT_NFC),
LintId::of(&unused_self::UNUSED_SELF),
LintId::of(&wildcard_imports::ENUM_GLOB_USE),
LintId::of(&wildcard_imports::WILDCARD_IMPORTS),
]);

View File

@ -1,11 +1,32 @@
use crate::utils::{in_macro, snippet_with_applicability, span_lint_and_sugg};
use if_chain::if_chain;
use rustc_errors::Applicability;
use rustc_hir::*;
use rustc_hir::{
def::{DefKind, Res},
Item, ItemKind, UseKind,
};
use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::BytePos;
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,
pedantic,
"use items that import all variants of an enum"
}
declare_clippy_lint! {
/// **What it does:** Checks for wildcard imports `use _::*`.
///
@ -45,7 +66,7 @@
"lint `use _::*` statements"
}
declare_lint_pass!(WildcardImports => [WILDCARD_IMPORTS]);
declare_lint_pass!(WildcardImports => [ENUM_GLOB_USE, WILDCARD_IMPORTS]);
impl LateLintPass<'_, '_> for WildcardImports {
fn check_item(&mut self, cx: &LateContext<'_, '_>, item: &Item<'_>) {
@ -94,11 +115,17 @@ fn check_item(&mut self, cx: &LateContext<'_, '_>, item: &Item<'_>) {
format!("{}::{}", import_source, imports_string)
};
let (lint, message) = if let Res::Def(DefKind::Enum, _) = use_path.res {
(ENUM_GLOB_USE, "usage of wildcard import for enum variants")
} else {
(WILDCARD_IMPORTS, "usage of wildcard import")
};
span_lint_and_sugg(
cx,
WILDCARD_IMPORTS,
lint,
span,
"usage of wildcard import",
message,
"try",
sugg,
applicability,

View File

@ -460,7 +460,7 @@
group: "pedantic",
desc: "use items that import all variants of an enum",
deprecation: None,
module: "enum_glob_use",
module: "wildcard_imports",
},
Lint {
name: "enum_variant_names",

View File

@ -0,0 +1,30 @@
// run-rustfix
#![warn(clippy::enum_glob_use)]
#![allow(unused)]
#![warn(unused_imports)]
use std::cmp::Ordering::Less;
enum Enum {
Foo,
}
use self::Enum::Foo;
mod in_fn_test {
fn blarg() {
use crate::Enum::Foo;
let _ = Foo;
}
}
mod blurg {
pub use std::cmp::Ordering::*; // ok, re-export
}
fn main() {
let _ = Foo;
let _ = Less;
}

View File

@ -1,29 +1,30 @@
#![warn(clippy::all, clippy::pedantic)]
#![allow(unused_imports, dead_code, clippy::missing_docs_in_private_items)]
// run-rustfix
#![warn(clippy::enum_glob_use)]
#![allow(unused)]
#![warn(unused_imports)]
use std::cmp::Ordering::*;
enum Enum {
_Foo,
Foo,
}
use self::Enum::*;
fn blarg() {
use self::Enum::*; // ok, just for a function
mod in_fn_test {
fn blarg() {
use crate::Enum::*;
let _ = Foo;
}
}
mod blurg {
pub use std::cmp::Ordering::*; // ok, re-export
}
mod tests {
use super::*;
fn main() {
let _ = Foo;
let _ = Less;
}
#[allow(non_snake_case)]
mod CamelCaseName {}
use CamelCaseName::*;
fn main() {}

View File

@ -1,16 +1,22 @@
error: don't use glob imports for enum variants
--> $DIR/enum_glob_use.rs:4:1
error: usage of wildcard import for enum variants
--> $DIR/enum_glob_use.rs:7:5
|
LL | use std::cmp::Ordering::*;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `std::cmp::Ordering::Less`
|
= note: `-D clippy::enum-glob-use` implied by `-D warnings`
error: don't use glob imports for enum variants
--> $DIR/enum_glob_use.rs:10:1
error: usage of wildcard import for enum variants
--> $DIR/enum_glob_use.rs:13:5
|
LL | use self::Enum::*;
| ^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^ help: try: `self::Enum::Foo`
error: aborting due to 2 previous errors
error: usage of wildcard import for enum variants
--> $DIR/enum_glob_use.rs:17:13
|
LL | use crate::Enum::*;
| ^^^^^^^^^^^^^^ help: try: `crate::Enum::Foo`
error: aborting due to 3 previous errors