1
1
use clippy_utils:: diagnostics:: { span_lint_and_help, span_lint_and_sugg} ;
2
2
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 } ;
4
4
use rustc_errors:: Applicability ;
5
5
use rustc_lint:: { EarlyContext , EarlyLintPass } ;
6
6
use rustc_session:: { declare_lint_pass, declare_tool_lint} ;
@@ -60,8 +60,21 @@ fn check_mod(cx: &EarlyContext<'_>, items: &[P<Item>]) {
60
60
// ```
61
61
let mut single_use_usages = Vec :: new ( ) ;
62
62
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
+
63
70
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
+ ) ;
65
78
}
66
79
67
80
for single_use in & single_use_usages {
@@ -96,6 +109,7 @@ fn track_uses(
96
109
item : & Item ,
97
110
imports_reused_with_self : & mut Vec < Symbol > ,
98
111
single_use_usages : & mut Vec < ( Symbol , Span , bool ) > ,
112
+ macros : & mut Vec < Symbol > ,
99
113
) {
100
114
if in_macro ( item. span ) || item. vis . kind . is_pub ( ) {
101
115
return ;
@@ -105,14 +119,22 @@ fn track_uses(
105
119
ItemKind :: Mod ( _, ModKind :: Loaded ( ref items, ..) ) => {
106
120
check_mod ( cx, & items) ;
107
121
} ,
122
+ ItemKind :: MacroDef ( MacroDef { macro_rules : true , .. } ) => {
123
+ macros. push ( item. ident . name ) ;
124
+ } ,
108
125
ItemKind :: Use ( use_tree) => {
109
126
let segments = & use_tree. prefix . segments ;
110
127
128
+ let should_report =
129
+ |name : & Symbol | !macros. contains ( name) || matches ! ( item. vis. kind, VisibilityKind :: Inherited ) ;
130
+
111
131
// keep track of `use some_module;` usages
112
132
if segments. len ( ) == 1 {
113
133
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
+ }
116
138
}
117
139
return ;
118
140
}
@@ -124,8 +146,10 @@ fn track_uses(
124
146
let segments = & tree. 0 . prefix . segments ;
125
147
if segments. len ( ) == 1 {
126
148
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
+ }
129
153
}
130
154
}
131
155
}
0 commit comments