2021-04-22 09:31:13 +00:00
|
|
|
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg};
|
2022-03-14 21:39:36 +00:00
|
|
|
use rustc_ast::{ptr::P, Crate, Item, ItemKind, MacroDef, ModKind, UseTreeKind};
|
2020-01-29 16:21:29 +00:00
|
|
|
use rustc_errors::Applicability;
|
2021-12-04 15:09:15 +00:00
|
|
|
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
|
2020-01-29 16:21:29 +00:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2021-04-22 09:31:13 +00:00
|
|
|
use rustc_span::{edition::Edition, symbol::kw, Span, Symbol};
|
2020-01-29 16:21:29 +00:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### What it does
|
|
|
|
/// Checking for imports with single component use path.
|
2020-01-29 16:21:29 +00:00
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Import with single component use path such as `use cratename;`
|
2020-01-29 16:21:29 +00:00
|
|
|
/// is not necessary, and thus should be removed.
|
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Example
|
2020-06-09 14:36:01 +00:00
|
|
|
/// ```rust,ignore
|
2020-01-29 16:21:29 +00:00
|
|
|
/// use regex;
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// regex::Regex::new(r"^\d{4}-\d{2}-\d{2}$").unwrap();
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
/// Better as
|
2020-06-09 14:36:01 +00:00
|
|
|
/// ```rust,ignore
|
2020-01-29 16:21:29 +00:00
|
|
|
/// fn main() {
|
|
|
|
/// regex::Regex::new(r"^\d{4}-\d{2}-\d{2}$").unwrap();
|
|
|
|
/// }
|
|
|
|
/// ```
|
2021-12-06 11:33:31 +00:00
|
|
|
#[clippy::version = "1.43.0"]
|
2020-01-29 16:21:29 +00:00
|
|
|
pub SINGLE_COMPONENT_PATH_IMPORTS,
|
|
|
|
style,
|
|
|
|
"imports with single component path are redundant"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint_pass!(SingleComponentPathImports => [SINGLE_COMPONENT_PATH_IMPORTS]);
|
|
|
|
|
|
|
|
impl EarlyLintPass for SingleComponentPathImports {
|
2021-04-22 09:31:13 +00:00
|
|
|
fn check_crate(&mut self, cx: &EarlyContext<'_>, krate: &Crate) {
|
2021-12-04 15:09:15 +00:00
|
|
|
if cx.sess().opts.edition < Edition::Edition2018 {
|
2021-04-22 09:31:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
check_mod(cx, &krate.items);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_mod(cx: &EarlyContext<'_>, items: &[P<Item>]) {
|
|
|
|
// 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};
|
|
|
|
// ```
|
|
|
|
let mut imports_reused_with_self = Vec::new();
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
2021-04-27 14:55:11 +00:00
|
|
|
// keep track of macros defined in the module as we don't want it to trigger on this (#7106)
|
|
|
|
// ```rust,ignore
|
|
|
|
// macro_rules! foo { () => {} };
|
|
|
|
// pub(crate) use foo;
|
|
|
|
// ```
|
|
|
|
let mut macros = Vec::new();
|
|
|
|
|
2021-04-22 09:31:13 +00:00
|
|
|
for item in items {
|
2021-04-27 14:55:11 +00:00
|
|
|
track_uses(
|
|
|
|
cx,
|
2021-06-03 06:41:37 +00:00
|
|
|
item,
|
2021-04-27 14:55:11 +00:00
|
|
|
&mut imports_reused_with_self,
|
|
|
|
&mut single_use_usages,
|
|
|
|
&mut macros,
|
|
|
|
);
|
2021-04-22 09:31:13 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 21:39:36 +00:00
|
|
|
for (name, span, can_suggest) in single_use_usages {
|
|
|
|
if !imports_reused_with_self.contains(&name) {
|
2021-04-22 09:31:13 +00:00
|
|
|
if can_suggest {
|
2020-01-29 16:21:29 +00:00
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
SINGLE_COMPONENT_PATH_IMPORTS,
|
2022-03-14 21:39:36 +00:00
|
|
|
span,
|
2020-01-29 16:21:29 +00:00
|
|
|
"this import is redundant",
|
|
|
|
"remove it entirely",
|
|
|
|
String::new(),
|
2021-04-22 09:31:13 +00:00
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
span_lint_and_help(
|
|
|
|
cx,
|
|
|
|
SINGLE_COMPONENT_PATH_IMPORTS,
|
2022-03-14 21:39:36 +00:00
|
|
|
span,
|
2021-04-22 09:31:13 +00:00
|
|
|
"this import is redundant",
|
|
|
|
None,
|
|
|
|
"remove this import",
|
2020-01-29 16:21:29 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-22 09:31:13 +00:00
|
|
|
|
|
|
|
fn track_uses(
|
|
|
|
cx: &EarlyContext<'_>,
|
|
|
|
item: &Item,
|
|
|
|
imports_reused_with_self: &mut Vec<Symbol>,
|
|
|
|
single_use_usages: &mut Vec<(Symbol, Span, bool)>,
|
2021-04-27 14:55:11 +00:00
|
|
|
macros: &mut Vec<Symbol>,
|
2021-04-22 09:31:13 +00:00
|
|
|
) {
|
2021-12-06 11:33:31 +00:00
|
|
|
if item.span.from_expansion() || item.vis.kind.is_pub() {
|
2021-04-22 09:31:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
match &item.kind {
|
|
|
|
ItemKind::Mod(_, ModKind::Loaded(ref items, ..)) => {
|
2021-06-03 06:41:37 +00:00
|
|
|
check_mod(cx, items);
|
2021-04-22 09:31:13 +00:00
|
|
|
},
|
2021-04-27 14:55:11 +00:00
|
|
|
ItemKind::MacroDef(MacroDef { macro_rules: true, .. }) => {
|
|
|
|
macros.push(item.ident.name);
|
|
|
|
},
|
2021-04-22 09:31:13 +00:00
|
|
|
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 {
|
2021-04-27 14:55:11 +00:00
|
|
|
let name = segments[0].ident.name;
|
2022-03-14 21:39:36 +00:00
|
|
|
if !macros.contains(&name) {
|
2021-04-27 14:55:11 +00:00
|
|
|
single_use_usages.push((name, item.span, true));
|
|
|
|
}
|
2021-04-22 09:31:13 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if segments.is_empty() {
|
|
|
|
// keep track of `use {some_module, some_other_module};` usages
|
|
|
|
if let UseTreeKind::Nested(trees) = &use_tree.kind {
|
|
|
|
for tree in trees {
|
|
|
|
let segments = &tree.0.prefix.segments;
|
|
|
|
if segments.len() == 1 {
|
|
|
|
if let UseTreeKind::Simple(None, _, _) = tree.0.kind {
|
2021-04-27 14:55:11 +00:00
|
|
|
let name = segments[0].ident.name;
|
2022-03-14 21:39:36 +00:00
|
|
|
if !macros.contains(&name) {
|
2021-04-27 14:55:11 +00:00
|
|
|
single_use_usages.push((name, tree.0.span, false));
|
|
|
|
}
|
2021-04-22 09:31:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// keep track of `use self::some_module` usages
|
|
|
|
if segments[0].ident.name == kw::SelfLower {
|
|
|
|
// simple case such as `use self::module::SomeStruct`
|
|
|
|
if segments.len() > 1 {
|
|
|
|
imports_reused_with_self.push(segments[1].ident.name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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() {
|
|
|
|
imports_reused_with_self.push(segments[0].ident.name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
}
|