Skip to content

Commit 06a6189

Browse files
committed
Move enum_glob_use lint into wildcard_imports pass
1 parent 3f5ed28 commit 06a6189

File tree

7 files changed

+92
-79
lines changed

7 files changed

+92
-79
lines changed

clippy_lints/src/enum_glob_use.rs

-49
This file was deleted.

clippy_lints/src/lib.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,6 @@ pub mod else_if_without_else;
197197
pub mod empty_enum;
198198
pub mod entry;
199199
pub mod enum_clike;
200-
pub mod enum_glob_use;
201200
pub mod enum_variants;
202201
pub mod eq_op;
203202
pub mod erasing_op;
@@ -520,7 +519,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
520519
&empty_enum::EMPTY_ENUM,
521520
&entry::MAP_ENTRY,
522521
&enum_clike::ENUM_CLIKE_UNPORTABLE_VARIANT,
523-
&enum_glob_use::ENUM_GLOB_USE,
524522
&enum_variants::ENUM_VARIANT_NAMES,
525523
&enum_variants::MODULE_INCEPTION,
526524
&enum_variants::MODULE_NAME_REPETITIONS,
@@ -814,6 +812,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
814812
&use_self::USE_SELF,
815813
&vec::USELESS_VEC,
816814
&wildcard_dependencies::WILDCARD_DEPENDENCIES,
815+
&wildcard_imports::ENUM_GLOB_USE,
817816
&wildcard_imports::WILDCARD_IMPORTS,
818817
&write::PRINTLN_EMPTY_STRING,
819818
&write::PRINT_LITERAL,
@@ -837,7 +836,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
837836
store.register_late_pass(move || box types::Types::new(vec_box_size_threshold));
838837
store.register_late_pass(|| box booleans::NonminimalBool);
839838
store.register_late_pass(|| box eq_op::EqOp);
840-
store.register_late_pass(|| box enum_glob_use::EnumGlobUse);
841839
store.register_late_pass(|| box enum_clike::UnportableVariant);
842840
store.register_late_pass(|| box float_literal::FloatLiteral);
843841
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:
10641062
LintId::of(&doc::DOC_MARKDOWN),
10651063
LintId::of(&doc::MISSING_ERRORS_DOC),
10661064
LintId::of(&empty_enum::EMPTY_ENUM),
1067-
LintId::of(&enum_glob_use::ENUM_GLOB_USE),
10681065
LintId::of(&enum_variants::MODULE_NAME_REPETITIONS),
10691066
LintId::of(&enum_variants::PUB_ENUM_VARIANT_NAMES),
10701067
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:
11081105
LintId::of(&unicode::NON_ASCII_LITERAL),
11091106
LintId::of(&unicode::UNICODE_NOT_NFC),
11101107
LintId::of(&unused_self::UNUSED_SELF),
1108+
LintId::of(&wildcard_imports::ENUM_GLOB_USE),
11111109
LintId::of(&wildcard_imports::WILDCARD_IMPORTS),
11121110
]);
11131111

clippy_lints/src/wildcard_imports.rs

+31-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,32 @@
11
use crate::utils::{in_macro, snippet_with_applicability, span_lint_and_sugg};
22
use if_chain::if_chain;
33
use rustc_errors::Applicability;
4-
use rustc_hir::*;
4+
use rustc_hir::{
5+
def::{DefKind, Res},
6+
Item, ItemKind, UseKind,
7+
};
58
use rustc_lint::{LateContext, LateLintPass};
69
use rustc_session::{declare_lint_pass, declare_tool_lint};
710
use rustc_span::BytePos;
811

12+
declare_clippy_lint! {
13+
/// **What it does:** Checks for `use Enum::*`.
14+
///
15+
/// **Why is this bad?** It is usually better style to use the prefixed name of
16+
/// an enumeration variant, rather than importing variants.
17+
///
18+
/// **Known problems:** Old-style enumerations that prefix the variants are
19+
/// still around.
20+
///
21+
/// **Example:**
22+
/// ```rust
23+
/// use std::cmp::Ordering::*;
24+
/// ```
25+
pub ENUM_GLOB_USE,
26+
pedantic,
27+
"use items that import all variants of an enum"
28+
}
29+
930
declare_clippy_lint! {
1031
/// **What it does:** Checks for wildcard imports `use _::*`.
1132
///
@@ -45,7 +66,7 @@ declare_clippy_lint! {
4566
"lint `use _::*` statements"
4667
}
4768

48-
declare_lint_pass!(WildcardImports => [WILDCARD_IMPORTS]);
69+
declare_lint_pass!(WildcardImports => [ENUM_GLOB_USE, WILDCARD_IMPORTS]);
4970

5071
impl LateLintPass<'_, '_> for WildcardImports {
5172
fn check_item(&mut self, cx: &LateContext<'_, '_>, item: &Item<'_>) {
@@ -94,11 +115,17 @@ impl LateLintPass<'_, '_> for WildcardImports {
94115
format!("{}::{}", import_source, imports_string)
95116
};
96117

118+
let (lint, message) = if let Res::Def(DefKind::Enum, _) = use_path.res {
119+
(ENUM_GLOB_USE, "usage of wildcard import for enum variants")
120+
} else {
121+
(WILDCARD_IMPORTS, "usage of wildcard import")
122+
};
123+
97124
span_lint_and_sugg(
98125
cx,
99-
WILDCARD_IMPORTS,
126+
lint,
100127
span,
101-
"usage of wildcard import",
128+
message,
102129
"try",
103130
sugg,
104131
applicability,

src/lintlist/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ pub const ALL_LINTS: [Lint; 357] = [
460460
group: "pedantic",
461461
desc: "use items that import all variants of an enum",
462462
deprecation: None,
463-
module: "enum_glob_use",
463+
module: "wildcard_imports",
464464
},
465465
Lint {
466466
name: "enum_variant_names",

tests/ui/enum_glob_use.fixed

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// run-rustfix
2+
3+
#![warn(clippy::enum_glob_use)]
4+
#![allow(unused)]
5+
#![warn(unused_imports)]
6+
7+
use std::cmp::Ordering::Less;
8+
9+
enum Enum {
10+
Foo,
11+
}
12+
13+
use self::Enum::Foo;
14+
15+
mod in_fn_test {
16+
fn blarg() {
17+
use crate::Enum::Foo;
18+
19+
let _ = Foo;
20+
}
21+
}
22+
23+
mod blurg {
24+
pub use std::cmp::Ordering::*; // ok, re-export
25+
}
26+
27+
fn main() {
28+
let _ = Foo;
29+
let _ = Less;
30+
}

tests/ui/enum_glob_use.rs

+15-14
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
1-
#![warn(clippy::all, clippy::pedantic)]
2-
#![allow(unused_imports, dead_code, clippy::missing_docs_in_private_items)]
1+
// run-rustfix
2+
3+
#![warn(clippy::enum_glob_use)]
4+
#![allow(unused)]
5+
#![warn(unused_imports)]
36

47
use std::cmp::Ordering::*;
58

69
enum Enum {
7-
_Foo,
10+
Foo,
811
}
912

1013
use self::Enum::*;
1114

12-
fn blarg() {
13-
use self::Enum::*; // ok, just for a function
15+
mod in_fn_test {
16+
fn blarg() {
17+
use crate::Enum::*;
18+
19+
let _ = Foo;
20+
}
1421
}
1522

1623
mod blurg {
1724
pub use std::cmp::Ordering::*; // ok, re-export
1825
}
1926

20-
mod tests {
21-
use super::*;
27+
fn main() {
28+
let _ = Foo;
29+
let _ = Less;
2230
}
23-
24-
#[allow(non_snake_case)]
25-
mod CamelCaseName {}
26-
27-
use CamelCaseName::*;
28-
29-
fn main() {}

tests/ui/enum_glob_use.stderr

+13-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1-
error: don't use glob imports for enum variants
2-
--> $DIR/enum_glob_use.rs:4:1
1+
error: usage of wildcard import for enum variants
2+
--> $DIR/enum_glob_use.rs:7:5
33
|
44
LL | use std::cmp::Ordering::*;
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `std::cmp::Ordering::Less`
66
|
77
= note: `-D clippy::enum-glob-use` implied by `-D warnings`
88

9-
error: don't use glob imports for enum variants
10-
--> $DIR/enum_glob_use.rs:10:1
9+
error: usage of wildcard import for enum variants
10+
--> $DIR/enum_glob_use.rs:13:5
1111
|
1212
LL | use self::Enum::*;
13-
| ^^^^^^^^^^^^^^^^^^
13+
| ^^^^^^^^^^^^^ help: try: `self::Enum::Foo`
1414

15-
error: aborting due to 2 previous errors
15+
error: usage of wildcard import for enum variants
16+
--> $DIR/enum_glob_use.rs:17:13
17+
|
18+
LL | use crate::Enum::*;
19+
| ^^^^^^^^^^^^^^ help: try: `crate::Enum::Foo`
20+
21+
error: aborting due to 3 previous errors
1622

0 commit comments

Comments
 (0)