Skip to content

Commit f4a8ecd

Browse files
committed
Move enum_glob_use lint into wildcard_imports pass
1 parent 3e610ea commit f4a8ecd

File tree

7 files changed

+92
-79
lines changed

7 files changed

+92
-79
lines changed

clippy_lints/src/enum_glob_use.rs

Lines changed: 0 additions & 49 deletions
This file was deleted.

clippy_lints/src/lib.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,6 @@ pub mod else_if_without_else;
195195
pub mod empty_enum;
196196
pub mod entry;
197197
pub mod enum_clike;
198-
pub mod enum_glob_use;
199198
pub mod enum_variants;
200199
pub mod eq_op;
201200
pub mod erasing_op;
@@ -518,7 +517,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
518517
&empty_enum::EMPTY_ENUM,
519518
&entry::MAP_ENTRY,
520519
&enum_clike::ENUM_CLIKE_UNPORTABLE_VARIANT,
521-
&enum_glob_use::ENUM_GLOB_USE,
522520
&enum_variants::ENUM_VARIANT_NAMES,
523521
&enum_variants::MODULE_INCEPTION,
524522
&enum_variants::MODULE_NAME_REPETITIONS,
@@ -811,6 +809,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
811809
&use_self::USE_SELF,
812810
&vec::USELESS_VEC,
813811
&wildcard_dependencies::WILDCARD_DEPENDENCIES,
812+
&wildcard_imports::ENUM_GLOB_USE,
814813
&wildcard_imports::WILDCARD_IMPORTS,
815814
&write::PRINTLN_EMPTY_STRING,
816815
&write::PRINT_LITERAL,
@@ -834,7 +833,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
834833
store.register_late_pass(move || box types::Types::new(vec_box_size_threshold));
835834
store.register_late_pass(|| box booleans::NonminimalBool);
836835
store.register_late_pass(|| box eq_op::EqOp);
837-
store.register_late_pass(|| box enum_glob_use::EnumGlobUse);
838836
store.register_late_pass(|| box enum_clike::UnportableVariant);
839837
store.register_late_pass(|| box excessive_precision::ExcessivePrecision);
840838
let verbose_bit_mask_threshold = conf.verbose_bit_mask_threshold;
@@ -1059,7 +1057,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
10591057
LintId::of(&doc::DOC_MARKDOWN),
10601058
LintId::of(&doc::MISSING_ERRORS_DOC),
10611059
LintId::of(&empty_enum::EMPTY_ENUM),
1062-
LintId::of(&enum_glob_use::ENUM_GLOB_USE),
10631060
LintId::of(&enum_variants::MODULE_NAME_REPETITIONS),
10641061
LintId::of(&enum_variants::PUB_ENUM_VARIANT_NAMES),
10651062
LintId::of(&eta_reduction::REDUNDANT_CLOSURE_FOR_METHOD_CALLS),
@@ -1103,6 +1100,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
11031100
LintId::of(&unicode::NON_ASCII_LITERAL),
11041101
LintId::of(&unicode::UNICODE_NOT_NFC),
11051102
LintId::of(&unused_self::UNUSED_SELF),
1103+
LintId::of(&wildcard_imports::ENUM_GLOB_USE),
11061104
LintId::of(&wildcard_imports::WILDCARD_IMPORTS),
11071105
]);
11081106

clippy_lints/src/wildcard_imports.rs

Lines changed: 31 additions & 4 deletions
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ pub const ALL_LINTS: [Lint; 356] = [
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

Lines changed: 30 additions & 0 deletions
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

Lines changed: 15 additions & 14 deletions
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

Lines changed: 13 additions & 7 deletions
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)