Skip to content

Commit

Permalink
Move enum_glob_use lint into wildcard_imports pass
Browse files Browse the repository at this point in the history
  • Loading branch information
flip1995 committed Jan 14, 2020
1 parent 369e62c commit 12937f0
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 78 deletions.
49 changes: 0 additions & 49 deletions clippy_lints/src/enum_glob_use.rs

This file was deleted.

6 changes: 2 additions & 4 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ pub mod else_if_without_else;
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;
Expand Down Expand Up @@ -516,7 +515,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,
Expand Down Expand Up @@ -801,6 +799,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,
Expand All @@ -823,7 +822,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
store.register_late_pass(|| box types::Types);
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 excessive_precision::ExcessivePrecision);
let verbose_bit_mask_threshold = conf.verbose_bit_mask_threshold;
Expand Down Expand Up @@ -1044,7 +1042,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),
Expand Down Expand Up @@ -1085,6 +1082,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),
]);

Expand Down
35 changes: 31 additions & 4 deletions clippy_lints/src/wildcard_imports.rs
Original file line number Diff line number Diff line change
@@ -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 _::*`.
///
Expand Down Expand Up @@ -45,7 +66,7 @@ declare_clippy_lint! {
"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<'_>) {
Expand Down Expand Up @@ -94,11 +115,17 @@ impl LateLintPass<'_, '_> for WildcardImports {
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,
Expand Down
30 changes: 30 additions & 0 deletions tests/ui/enum_glob_use.fixed
Original file line number Diff line number Diff line change
@@ -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;
}
29 changes: 15 additions & 14 deletions tests/ui/enum_glob_use.rs
Original file line number Diff line number Diff line change
@@ -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() {}
20 changes: 13 additions & 7 deletions tests/ui/enum_glob_use.stderr
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 12937f0

Please sign in to comment.