mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-23 05:03:21 +00:00
Move enum_glob_use lint into wildcard_imports pass
This commit is contained in:
parent
3f5ed28524
commit
06a6189376
7 changed files with 92 additions and 79 deletions
|
@ -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");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
|
@ -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),
|
||||
]);
|
||||
|
||||
|
|
|
@ -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 @@ 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<'_>) {
|
||||
|
@ -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,
|
||||
|
|
|
@ -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",
|
||||
|
|
30
tests/ui/enum_glob_use.fixed
Normal file
30
tests/ui/enum_glob_use.fixed
Normal 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;
|
||||
}
|
|
@ -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() {}
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue