use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg};
-use clippy_utils::in_macro;
-use rustc_ast::{ptr::P, Crate, Item, ItemKind, ModKind, UseTreeKind};
+use rustc_ast::{ptr::P, Crate, Item, ItemKind, MacroDef, ModKind, UseTreeKind, VisibilityKind};
use rustc_errors::Applicability;
-use rustc_lint::{EarlyContext, EarlyLintPass};
+use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::{edition::Edition, symbol::kw, Span, Symbol};
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.
///
- /// **Why is this bad?** Import with single component use path such as `use cratename;`
+ /// ### Why is this bad?
+ /// Import with single component use path such as `use cratename;`
/// is not necessary, and thus should be removed.
///
- /// **Known problems:** None.
- ///
- /// **Example:**
- ///
+ /// ### Example
/// ```rust,ignore
/// use regex;
///
/// regex::Regex::new(r"^\d{4}-\d{2}-\d{2}$").unwrap();
/// }
/// ```
+ #[clippy::version = "1.43.0"]
pub SINGLE_COMPONENT_PATH_IMPORTS,
style,
"imports with single component path are redundant"
impl EarlyLintPass for SingleComponentPathImports {
fn check_crate(&mut self, cx: &EarlyContext<'_>, krate: &Crate) {
- if cx.sess.opts.edition < Edition::Edition2018 {
+ if cx.sess().opts.edition < Edition::Edition2018 {
return;
}
check_mod(cx, &krate.items);
// ```
let mut single_use_usages = Vec::new();
+ // 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();
+
for item in items {
- track_uses(cx, &item, &mut imports_reused_with_self, &mut single_use_usages);
+ track_uses(
+ cx,
+ item,
+ &mut imports_reused_with_self,
+ &mut single_use_usages,
+ &mut macros,
+ );
}
for single_use in &single_use_usages {
item: &Item,
imports_reused_with_self: &mut Vec<Symbol>,
single_use_usages: &mut Vec<(Symbol, Span, bool)>,
+ macros: &mut Vec<Symbol>,
) {
- if in_macro(item.span) || item.vis.kind.is_pub() {
+ if item.span.from_expansion() || item.vis.kind.is_pub() {
return;
}
match &item.kind {
ItemKind::Mod(_, ModKind::Loaded(ref items, ..)) => {
- check_mod(cx, &items);
+ check_mod(cx, items);
+ },
+ ItemKind::MacroDef(MacroDef { macro_rules: true, .. }) => {
+ macros.push(item.ident.name);
},
ItemKind::Use(use_tree) => {
let segments = &use_tree.prefix.segments;
+ let should_report =
+ |name: &Symbol| !macros.contains(name) || matches!(item.vis.kind, VisibilityKind::Inherited);
+
// 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, true));
+ let name = segments[0].ident.name;
+ if should_report(&name) {
+ single_use_usages.push((name, item.span, true));
+ }
}
return;
}
let segments = &tree.0.prefix.segments;
if segments.len() == 1 {
if let UseTreeKind::Simple(None, _, _) = tree.0.kind {
- let ident = &segments[0].ident;
- single_use_usages.push((ident.name, tree.0.span, false));
+ let name = segments[0].ident.name;
+ if should_report(&name) {
+ single_use_usages.push((name, tree.0.span, false));
+ }
}
}
}