Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Move enum_glob_use lint into wildcard_imports pass
  • Loading branch information
flip1995 committed Feb 21, 2020
commit 06a6189376975ddff0d8db2fb20ab407066357b4
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 @@ -197,7 +197,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 @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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),
]);

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
2 changes: 1 addition & 1 deletion src/lintlist/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ pub const ALL_LINTS: [Lint; 357] = [
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",
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