Skip to content

Commit 74f5599

Browse files
committed
Auto merge of #7120 - cherryblossom000:7106, r=Manishearth
`single_component_path_imports`: ignore `pub(crate) use some_macro;` Fixes #7106 *Please write a short comment explaining your change (or "none" for internal only changes)* changelog: Ignore exporting a macro within a crate using `pub(crate) use some_macro;` for [`single_component_path_imports`]
2 parents 0ab7acc + b48699e commit 74f5599

4 files changed

+82
-6
lines changed

clippy_lints/src/single_component_path_imports.rs

+30-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_sugg};
22
use clippy_utils::in_macro;
3-
use rustc_ast::{ptr::P, Crate, Item, ItemKind, ModKind, UseTreeKind};
3+
use rustc_ast::{ptr::P, Crate, Item, ItemKind, MacroDef, ModKind, UseTreeKind, VisibilityKind};
44
use rustc_errors::Applicability;
55
use rustc_lint::{EarlyContext, EarlyLintPass};
66
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -60,8 +60,21 @@ fn check_mod(cx: &EarlyContext<'_>, items: &[P<Item>]) {
6060
// ```
6161
let mut single_use_usages = Vec::new();
6262

63+
// keep track of macros defined in the module as we don't want it to trigger on this (#7106)
64+
// ```rust,ignore
65+
// macro_rules! foo { () => {} };
66+
// pub(crate) use foo;
67+
// ```
68+
let mut macros = Vec::new();
69+
6370
for item in items {
64-
track_uses(cx, &item, &mut imports_reused_with_self, &mut single_use_usages);
71+
track_uses(
72+
cx,
73+
&item,
74+
&mut imports_reused_with_self,
75+
&mut single_use_usages,
76+
&mut macros,
77+
);
6578
}
6679

6780
for single_use in &single_use_usages {
@@ -96,6 +109,7 @@ fn track_uses(
96109
item: &Item,
97110
imports_reused_with_self: &mut Vec<Symbol>,
98111
single_use_usages: &mut Vec<(Symbol, Span, bool)>,
112+
macros: &mut Vec<Symbol>,
99113
) {
100114
if in_macro(item.span) || item.vis.kind.is_pub() {
101115
return;
@@ -105,14 +119,22 @@ fn track_uses(
105119
ItemKind::Mod(_, ModKind::Loaded(ref items, ..)) => {
106120
check_mod(cx, &items);
107121
},
122+
ItemKind::MacroDef(MacroDef { macro_rules: true, .. }) => {
123+
macros.push(item.ident.name);
124+
},
108125
ItemKind::Use(use_tree) => {
109126
let segments = &use_tree.prefix.segments;
110127

128+
let should_report =
129+
|name: &Symbol| !macros.contains(name) || matches!(item.vis.kind, VisibilityKind::Inherited);
130+
111131
// keep track of `use some_module;` usages
112132
if segments.len() == 1 {
113133
if let UseTreeKind::Simple(None, _, _) = use_tree.kind {
114-
let ident = &segments[0].ident;
115-
single_use_usages.push((ident.name, item.span, true));
134+
let name = segments[0].ident.name;
135+
if should_report(&name) {
136+
single_use_usages.push((name, item.span, true));
137+
}
116138
}
117139
return;
118140
}
@@ -124,8 +146,10 @@ fn track_uses(
124146
let segments = &tree.0.prefix.segments;
125147
if segments.len() == 1 {
126148
if let UseTreeKind::Simple(None, _, _) = tree.0.kind {
127-
let ident = &segments[0].ident;
128-
single_use_usages.push((ident.name, tree.0.span, false));
149+
let name = segments[0].ident.name;
150+
if should_report(&name) {
151+
single_use_usages.push((name, tree.0.span, false));
152+
}
129153
}
130154
}
131155
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// run-rustfix
2+
// edition:2018
3+
#![warn(clippy::single_component_path_imports)]
4+
#![allow(unused_imports)]
5+
6+
// #7106: use statements exporting a macro within a crate should not trigger lint
7+
8+
macro_rules! m1 {
9+
() => {};
10+
}
11+
pub(crate) use m1; // ok
12+
13+
macro_rules! m2 {
14+
() => {};
15+
}
16+
// fail
17+
18+
fn main() {
19+
m1!();
20+
m2!();
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// run-rustfix
2+
// edition:2018
3+
#![warn(clippy::single_component_path_imports)]
4+
#![allow(unused_imports)]
5+
6+
// #7106: use statements exporting a macro within a crate should not trigger lint
7+
8+
macro_rules! m1 {
9+
() => {};
10+
}
11+
pub(crate) use m1; // ok
12+
13+
macro_rules! m2 {
14+
() => {};
15+
}
16+
use m2; // fail
17+
18+
fn main() {
19+
m1!();
20+
m2!();
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: this import is redundant
2+
--> $DIR/single_component_path_imports_macro.rs:16:1
3+
|
4+
LL | use m2; // fail
5+
| ^^^^^^^ help: remove it entirely
6+
|
7+
= note: `-D clippy::single-component-path-imports` implied by `-D warnings`
8+
9+
error: aborting due to previous error
10+

0 commit comments

Comments
 (0)