Skip to content

Allow single_component_path_imports for all macros #8537

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions clippy_lints/src/single_component_path_imports.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg};
use rustc_ast::{ptr::P, Crate, Item, ItemKind, MacroDef, ModKind, UseTreeKind, VisibilityKind};
use rustc_ast::{ptr::P, Crate, Item, ItemKind, MacroDef, ModKind, UseTreeKind};
use rustc_errors::Applicability;
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
use rustc_session::{declare_lint_pass, declare_tool_lint};
Expand Down Expand Up @@ -76,14 +76,13 @@ fn check_mod(cx: &EarlyContext<'_>, items: &[P<Item>]) {
);
}

for single_use in &single_use_usages {
if !imports_reused_with_self.contains(&single_use.0) {
let can_suggest = single_use.2;
for (name, span, can_suggest) in single_use_usages {
if !imports_reused_with_self.contains(&name) {
if can_suggest {
span_lint_and_sugg(
cx,
SINGLE_COMPONENT_PATH_IMPORTS,
single_use.1,
span,
"this import is redundant",
"remove it entirely",
String::new(),
Expand All @@ -93,7 +92,7 @@ fn check_mod(cx: &EarlyContext<'_>, items: &[P<Item>]) {
span_lint_and_help(
cx,
SINGLE_COMPONENT_PATH_IMPORTS,
single_use.1,
span,
"this import is redundant",
None,
"remove this import",
Expand Down Expand Up @@ -124,14 +123,11 @@ fn track_uses(
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 name = segments[0].ident.name;
if should_report(&name) {
if !macros.contains(&name) {
single_use_usages.push((name, item.span, true));
}
}
Expand All @@ -146,7 +142,7 @@ fn track_uses(
if segments.len() == 1 {
if let UseTreeKind::Simple(None, _, _) = tree.0.kind {
let name = segments[0].ident.name;
if should_report(&name) {
if !macros.contains(&name) {
single_use_usages.push((name, tree.0.span, false));
}
}
Expand Down
20 changes: 0 additions & 20 deletions tests/ui/single_component_path_imports_macro.fixed

This file was deleted.

4 changes: 2 additions & 2 deletions tests/ui/single_component_path_imports_macro.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// run-rustfix
#![warn(clippy::single_component_path_imports)]
#![allow(unused_imports)]

// #7106: use statements exporting a macro within a crate should not trigger lint
// #7923: normal `use` statements of macros should also not trigger the lint

macro_rules! m1 {
() => {};
Expand All @@ -12,7 +12,7 @@ pub(crate) use m1; // ok
macro_rules! m2 {
() => {};
}
use m2; // fail
use m2; // ok

fn main() {
m1!();
Expand Down
10 changes: 0 additions & 10 deletions tests/ui/single_component_path_imports_macro.stderr

This file was deleted.