Check path imports per module
This commit is contained in:
@@ -1232,7 +1232,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
|
|||||||
store.register_early_pass(|| box as_conversions::AsConversions);
|
store.register_early_pass(|| box as_conversions::AsConversions);
|
||||||
store.register_late_pass(|| box let_underscore::LetUnderscore);
|
store.register_late_pass(|| box let_underscore::LetUnderscore);
|
||||||
store.register_late_pass(|| box atomic_ordering::AtomicOrdering);
|
store.register_late_pass(|| box atomic_ordering::AtomicOrdering);
|
||||||
store.register_early_pass(|| box single_component_path_imports::SingleComponentPathImports::default());
|
store.register_early_pass(|| box single_component_path_imports::SingleComponentPathImports);
|
||||||
let max_fn_params_bools = conf.max_fn_params_bools;
|
let max_fn_params_bools = conf.max_fn_params_bools;
|
||||||
let max_struct_bools = conf.max_struct_bools;
|
let max_struct_bools = conf.max_struct_bools;
|
||||||
store.register_early_pass(move || box excessive_bools::ExcessiveBools::new(max_struct_bools, max_fn_params_bools));
|
store.register_early_pass(move || box excessive_bools::ExcessiveBools::new(max_struct_bools, max_fn_params_bools));
|
||||||
|
|||||||
@@ -1,12 +1,10 @@
|
|||||||
use clippy_utils::diagnostics::span_lint_and_sugg;
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
||||||
use clippy_utils::in_macro;
|
use clippy_utils::in_macro;
|
||||||
use rustc_ast::{Crate, Item, ItemKind, ModKind, UseTreeKind};
|
use rustc_ast::{ptr::P, Crate, Item, ItemKind, ModKind, UseTreeKind};
|
||||||
use rustc_errors::Applicability;
|
use rustc_errors::Applicability;
|
||||||
use rustc_lint::{EarlyContext, EarlyLintPass};
|
use rustc_lint::{EarlyContext, EarlyLintPass};
|
||||||
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
||||||
use rustc_span::edition::Edition;
|
use rustc_span::{edition::Edition, symbol::kw, Span, Symbol};
|
||||||
use rustc_span::symbol::kw;
|
|
||||||
use rustc_span::{Span, Symbol};
|
|
||||||
|
|
||||||
declare_clippy_lint! {
|
declare_clippy_lint! {
|
||||||
/// **What it does:** Checking for imports with single component use path.
|
/// **What it does:** Checking for imports with single component use path.
|
||||||
@@ -36,94 +34,96 @@ declare_clippy_lint! {
|
|||||||
"imports with single component path are redundant"
|
"imports with single component path are redundant"
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
declare_lint_pass!(SingleComponentPathImports => [SINGLE_COMPONENT_PATH_IMPORTS]);
|
||||||
pub struct SingleComponentPathImports {
|
|
||||||
/// keep track of imports reused with `self` keyword,
|
|
||||||
/// such as `self::crypto_hash` in the example below
|
|
||||||
///
|
|
||||||
/// ```rust,ignore
|
|
||||||
/// use self::crypto_hash::{Algorithm, Hasher};
|
|
||||||
/// ```
|
|
||||||
imports_reused_with_self: Vec<Symbol>,
|
|
||||||
/// keep track of single use statements
|
|
||||||
/// such as `crypto_hash` in the example below
|
|
||||||
///
|
|
||||||
/// ```rust,ignore
|
|
||||||
/// use crypto_hash;
|
|
||||||
/// ```
|
|
||||||
single_use_usages: Vec<(Symbol, Span)>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl_lint_pass!(SingleComponentPathImports => [SINGLE_COMPONENT_PATH_IMPORTS]);
|
|
||||||
|
|
||||||
impl EarlyLintPass for SingleComponentPathImports {
|
impl EarlyLintPass for SingleComponentPathImports {
|
||||||
fn check_crate(&mut self, cx: &EarlyContext<'_>, krate: &Crate) {
|
fn check_crate(&mut self, cx: &EarlyContext<'_>, krate: &Crate) {
|
||||||
if cx.sess.opts.edition < Edition::Edition2018 {
|
if cx.sess.opts.edition < Edition::Edition2018 {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for item in &krate.items {
|
check_mod(cx, &krate.items);
|
||||||
self.track_uses(&item);
|
}
|
||||||
}
|
}
|
||||||
for single_use in &self.single_use_usages {
|
|
||||||
if !self.imports_reused_with_self.contains(&single_use.0) {
|
fn check_mod(cx: &EarlyContext<'_>, items: &[P<Item>]) {
|
||||||
span_lint_and_sugg(
|
// keep track of imports reused with `self` keyword,
|
||||||
cx,
|
// such as `self::crypto_hash` in the example below
|
||||||
SINGLE_COMPONENT_PATH_IMPORTS,
|
// ```rust,ignore
|
||||||
single_use.1,
|
// use self::crypto_hash::{Algorithm, Hasher};
|
||||||
"this import is redundant",
|
// ```
|
||||||
"remove it entirely",
|
let mut imports_reused_with_self = Vec::new();
|
||||||
String::new(),
|
|
||||||
Applicability::MachineApplicable,
|
// keep track of single use statements
|
||||||
);
|
// such as `crypto_hash` in the example below
|
||||||
}
|
// ```rust,ignore
|
||||||
|
// use crypto_hash;
|
||||||
|
// ```
|
||||||
|
let mut single_use_usages = Vec::new();
|
||||||
|
|
||||||
|
for item in items {
|
||||||
|
track_uses(cx, &item, &mut imports_reused_with_self, &mut single_use_usages);
|
||||||
|
}
|
||||||
|
|
||||||
|
for single_use in &single_use_usages {
|
||||||
|
if !imports_reused_with_self.contains(&single_use.0) {
|
||||||
|
span_lint_and_sugg(
|
||||||
|
cx,
|
||||||
|
SINGLE_COMPONENT_PATH_IMPORTS,
|
||||||
|
single_use.1,
|
||||||
|
"this import is redundant",
|
||||||
|
"remove it entirely",
|
||||||
|
String::new(),
|
||||||
|
Applicability::MachineApplicable,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SingleComponentPathImports {
|
fn track_uses(
|
||||||
fn track_uses(&mut self, item: &Item) {
|
cx: &EarlyContext<'_>,
|
||||||
if in_macro(item.span) || item.vis.kind.is_pub() {
|
item: &Item,
|
||||||
return;
|
imports_reused_with_self: &mut Vec<Symbol>,
|
||||||
}
|
single_use_usages: &mut Vec<(Symbol, Span)>,
|
||||||
|
) {
|
||||||
|
if in_macro(item.span) || item.vis.kind.is_pub() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
match &item.kind {
|
match &item.kind {
|
||||||
ItemKind::Mod(_, ModKind::Loaded(ref items, ..)) => {
|
ItemKind::Mod(_, ModKind::Loaded(ref items, ..)) => {
|
||||||
for item in items.iter() {
|
check_mod(cx, &items);
|
||||||
self.track_uses(&item);
|
},
|
||||||
|
ItemKind::Use(use_tree) => {
|
||||||
|
let segments = &use_tree.prefix.segments;
|
||||||
|
|
||||||
|
// keep track of `use some_module;` usages
|
||||||
|
if segments.len() == 1 {
|
||||||
|
if let UseTreeKind::Simple(None, _, _) = use_tree.kind {
|
||||||
|
let ident = &segments[0].ident;
|
||||||
|
single_use_usages.push((ident.name, item.span));
|
||||||
}
|
}
|
||||||
},
|
return;
|
||||||
ItemKind::Use(use_tree) => {
|
}
|
||||||
let segments = &use_tree.prefix.segments;
|
|
||||||
|
|
||||||
// keep track of `use some_module;` usages
|
// keep track of `use self::some_module` usages
|
||||||
if segments.len() == 1 {
|
if segments[0].ident.name == kw::SelfLower {
|
||||||
if let UseTreeKind::Simple(None, _, _) = use_tree.kind {
|
// simple case such as `use self::module::SomeStruct`
|
||||||
let ident = &segments[0].ident;
|
if segments.len() > 1 {
|
||||||
self.single_use_usages.push((ident.name, item.span));
|
imports_reused_with_self.push(segments[1].ident.name);
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// keep track of `use self::some_module` usages
|
// nested case such as `use self::{module1::Struct1, module2::Struct2}`
|
||||||
if segments[0].ident.name == kw::SelfLower {
|
if let UseTreeKind::Nested(trees) = &use_tree.kind {
|
||||||
// simple case such as `use self::module::SomeStruct`
|
for tree in trees {
|
||||||
if segments.len() > 1 {
|
let segments = &tree.0.prefix.segments;
|
||||||
self.imports_reused_with_self.push(segments[1].ident.name);
|
if !segments.is_empty() {
|
||||||
return;
|
imports_reused_with_self.push(segments[0].ident.name);
|
||||||
}
|
|
||||||
|
|
||||||
// nested case such as `use self::{module1::Struct1, module2::Struct2}`
|
|
||||||
if let UseTreeKind::Nested(trees) = &use_tree.kind {
|
|
||||||
for tree in trees {
|
|
||||||
let segments = &tree.0.prefix.segments;
|
|
||||||
if !segments.is_empty() {
|
|
||||||
self.imports_reused_with_self.push(segments[0].ident.name);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
_ => {},
|
},
|
||||||
}
|
_ => {},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,3 +25,10 @@ mod hello_mod {
|
|||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
fn hello_mod() {}
|
fn hello_mod() {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mod hi_mod {
|
||||||
|
use self::regex::{Regex, RegexSet};
|
||||||
|
use regex;
|
||||||
|
#[allow(dead_code)]
|
||||||
|
fn hi_mod() {}
|
||||||
|
}
|
||||||
|
|||||||
@@ -25,3 +25,10 @@ mod hello_mod {
|
|||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
fn hello_mod() {}
|
fn hello_mod() {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mod hi_mod {
|
||||||
|
use self::regex::{Regex, RegexSet};
|
||||||
|
use regex;
|
||||||
|
#[allow(dead_code)]
|
||||||
|
fn hi_mod() {}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,16 +1,16 @@
|
|||||||
error: this import is redundant
|
|
||||||
--> $DIR/single_component_path_imports.rs:6:1
|
|
||||||
|
|
|
||||||
LL | use regex;
|
|
||||||
| ^^^^^^^^^^ help: remove it entirely
|
|
||||||
|
|
|
||||||
= note: `-D clippy::single-component-path-imports` implied by `-D warnings`
|
|
||||||
|
|
||||||
error: this import is redundant
|
error: this import is redundant
|
||||||
--> $DIR/single_component_path_imports.rs:24:5
|
--> $DIR/single_component_path_imports.rs:24:5
|
||||||
|
|
|
|
||||||
LL | use regex;
|
LL | use regex;
|
||||||
| ^^^^^^^^^^ help: remove it entirely
|
| ^^^^^^^^^^ help: remove it entirely
|
||||||
|
|
|
||||||
|
= note: `-D clippy::single-component-path-imports` implied by `-D warnings`
|
||||||
|
|
||||||
|
error: this import is redundant
|
||||||
|
--> $DIR/single_component_path_imports.rs:6:1
|
||||||
|
|
|
||||||
|
LL | use regex;
|
||||||
|
| ^^^^^^^^^^ help: remove it entirely
|
||||||
|
|
||||||
error: aborting due to 2 previous errors
|
error: aborting due to 2 previous errors
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user