Skip to content

Commit 9a1f6a9

Browse files
committed
Auto merge of #8537 - xFrednet:7923-single-component-path-imports-for-macros, r=llogiq
Allow `single_component_path_imports` for all macros Closes: #7923 It can be useful to have `use macro_name` regardless of the visibility. This removes the visibility filter. changelog: [`single_component_path_imports`]: no longer triggers on macros
2 parents 0e1311b + 2ee5372 commit 9a1f6a9

4 files changed

+9
-43
lines changed

clippy_lints/src/single_component_path_imports.rs

+7-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg};
2-
use rustc_ast::{ptr::P, Crate, Item, ItemKind, MacroDef, ModKind, UseTreeKind, VisibilityKind};
2+
use rustc_ast::{ptr::P, Crate, Item, ItemKind, MacroDef, ModKind, UseTreeKind};
33
use rustc_errors::Applicability;
44
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
55
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -76,14 +76,13 @@ fn check_mod(cx: &EarlyContext<'_>, items: &[P<Item>]) {
7676
);
7777
}
7878

79-
for single_use in &single_use_usages {
80-
if !imports_reused_with_self.contains(&single_use.0) {
81-
let can_suggest = single_use.2;
79+
for (name, span, can_suggest) in single_use_usages {
80+
if !imports_reused_with_self.contains(&name) {
8281
if can_suggest {
8382
span_lint_and_sugg(
8483
cx,
8584
SINGLE_COMPONENT_PATH_IMPORTS,
86-
single_use.1,
85+
span,
8786
"this import is redundant",
8887
"remove it entirely",
8988
String::new(),
@@ -93,7 +92,7 @@ fn check_mod(cx: &EarlyContext<'_>, items: &[P<Item>]) {
9392
span_lint_and_help(
9493
cx,
9594
SINGLE_COMPONENT_PATH_IMPORTS,
96-
single_use.1,
95+
span,
9796
"this import is redundant",
9897
None,
9998
"remove this import",
@@ -124,14 +123,11 @@ fn track_uses(
124123
ItemKind::Use(use_tree) => {
125124
let segments = &use_tree.prefix.segments;
126125

127-
let should_report =
128-
|name: &Symbol| !macros.contains(name) || matches!(item.vis.kind, VisibilityKind::Inherited);
129-
130126
// keep track of `use some_module;` usages
131127
if segments.len() == 1 {
132128
if let UseTreeKind::Simple(None, _, _) = use_tree.kind {
133129
let name = segments[0].ident.name;
134-
if should_report(&name) {
130+
if !macros.contains(&name) {
135131
single_use_usages.push((name, item.span, true));
136132
}
137133
}
@@ -146,7 +142,7 @@ fn track_uses(
146142
if segments.len() == 1 {
147143
if let UseTreeKind::Simple(None, _, _) = tree.0.kind {
148144
let name = segments[0].ident.name;
149-
if should_report(&name) {
145+
if !macros.contains(&name) {
150146
single_use_usages.push((name, tree.0.span, false));
151147
}
152148
}

tests/ui/single_component_path_imports_macro.fixed

-20
This file was deleted.

tests/ui/single_component_path_imports_macro.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
// run-rustfix
21
#![warn(clippy::single_component_path_imports)]
32
#![allow(unused_imports)]
43

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

77
macro_rules! m1 {
88
() => {};
@@ -12,7 +12,7 @@ pub(crate) use m1; // ok
1212
macro_rules! m2 {
1313
() => {};
1414
}
15-
use m2; // fail
15+
use m2; // ok
1616

1717
fn main() {
1818
m1!();

tests/ui/single_component_path_imports_macro.stderr

-10
This file was deleted.

0 commit comments

Comments
 (0)