@@ -7,6 +7,7 @@ use hir::def::{Res, DefKind};
7
7
use rustc_lint:: { LintContext , LateLintPass , LateContext } ;
8
8
use rustc_session:: { declare_tool_lint, impl_lint_pass} ;
9
9
use rustc_span:: { edition:: Edition , Span } ;
10
+ use rustc_ast:: ast;
10
11
11
12
const PRELUDE : & [ & str ] = & [
12
13
"marker" , "ops" , "convert" , "iter" , "option" , "result" , "borrow" , "boxed" , "string" , "vec" , "macros" ,
@@ -97,10 +98,14 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
97
98
. find( |attr| attr. ident( ) . map( |s| s. to_string( ) ) == Some ( "macro_use" . to_string( ) ) ) ;
98
99
if let Res :: Def ( DefKind :: Mod , id) = path. res;
99
100
then {
101
+ // println!("{:#?}", lcx.tcx.def_path_str(id));
100
102
for kid in lcx. tcx. item_children( id) . iter( ) {
103
+ // println!("{:#?}", kid);
101
104
if let Res :: Def ( DefKind :: Macro ( _mac_type) , mac_id) = kid. res {
102
105
let span = mac_attr. span. clone( ) ;
103
- println!( "{:#?}" , lcx. tcx. def_path_str( mac_id) ) ;
106
+
107
+ // println!("{:#?}", lcx.tcx.def_path_str(mac_id));
108
+
104
109
self . imports. push( ( lcx. tcx. def_path_str( mac_id) , span) ) ;
105
110
}
106
111
}
@@ -113,24 +118,49 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
113
118
let mac = MacroRefData :: new( name. to_string( ) , callee. def_site, lcx) ;
114
119
self . mac_refs. push( ( call_site, mac. clone( ) ) ) ;
115
120
self . collected. insert( call_site, mac) ;
116
- // println!("EXPR {:?} {:?}", name, lcx.sess().source_map().span_to_filename(callee.def_site));
117
121
}
118
122
}
119
123
}
120
124
}
121
125
}
122
126
}
127
+ fn check_attribute ( & mut self , lcx : & LateContext < ' _ , ' _ > , attr : & ast:: Attribute ) {
128
+ if in_macro ( attr. span ) {
129
+ let call_site = attr. span . source_callsite ( ) ;
130
+ let name = snippet ( lcx, lcx. sess ( ) . source_map ( ) . span_until_char ( call_site, '!' ) , "_" ) ;
131
+ if let Some ( callee) = attr. span . source_callee ( ) {
132
+ if !self . collected . contains_key ( & call_site) {
133
+ println ! ( "{:?}\n {:#?}" , call_site, attr) ;
134
+
135
+ let name = if name. contains ( "::" ) {
136
+ name. split ( "::" ) . last ( ) . unwrap ( ) . to_string ( )
137
+ } else {
138
+ name. to_string ( )
139
+ } ;
123
140
141
+ let mac = MacroRefData :: new ( name, callee. def_site , lcx) ;
142
+ self . mac_refs . push ( ( call_site, mac. clone ( ) ) ) ;
143
+ self . collected . insert ( call_site, mac) ;
144
+ }
145
+ }
146
+ }
147
+ }
124
148
fn check_expr ( & mut self , lcx : & LateContext < ' _ , ' _ > , expr : & hir:: Expr < ' _ > ) {
125
149
if in_macro ( expr. span ) {
126
150
let call_site = expr. span . source_callsite ( ) ;
127
151
let name = snippet ( lcx, lcx. sess ( ) . source_map ( ) . span_until_char ( call_site, '!' ) , "_" ) ;
128
152
if let Some ( callee) = expr. span . source_callee ( ) {
129
153
if !self . collected . contains_key ( & call_site) {
130
- let mac = MacroRefData :: new ( name. to_string ( ) , callee. def_site , lcx) ;
154
+
155
+ let name = if name. contains ( "::" ) {
156
+ name. split ( "::" ) . last ( ) . unwrap ( ) . to_string ( )
157
+ } else {
158
+ name. to_string ( )
159
+ } ;
160
+
161
+ let mac = MacroRefData :: new ( name, callee. def_site , lcx) ;
131
162
self . mac_refs . push ( ( call_site, mac. clone ( ) ) ) ;
132
163
self . collected . insert ( call_site, mac) ;
133
- // println!("EXPR {:?} {:?}", name, lcx.sess().source_map().span_to_filename(callee.def_site));
134
164
}
135
165
}
136
166
}
@@ -141,10 +171,16 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
141
171
let name = snippet ( lcx, lcx. sess ( ) . source_map ( ) . span_until_char ( call_site, '!' ) , "_" ) ;
142
172
if let Some ( callee) = stmt. span . source_callee ( ) {
143
173
if !self . collected . contains_key ( & call_site) {
144
- let mac = MacroRefData :: new ( name. to_string ( ) , callee. def_site , lcx) ;
174
+
175
+ let name = if name. contains ( "::" ) {
176
+ name. split ( "::" ) . last ( ) . unwrap ( ) . to_string ( )
177
+ } else {
178
+ name. to_string ( )
179
+ } ;
180
+
181
+ let mac = MacroRefData :: new ( name, callee. def_site , lcx) ;
145
182
self . mac_refs . push ( ( call_site, mac. clone ( ) ) ) ;
146
183
self . collected . insert ( call_site, mac) ;
147
- // println!("STMT {:?} {:?}", name, lcx.sess().source_map().span_to_filename(callee.def_site));
148
184
}
149
185
}
150
186
}
@@ -158,7 +194,6 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
158
194
let mac = MacroRefData :: new ( name. to_string ( ) , callee. def_site , lcx) ;
159
195
self . mac_refs . push ( ( call_site, mac. clone ( ) ) ) ;
160
196
self . collected . insert ( call_site, mac) ;
161
- // println!("PAT {:?} {:?}", name, lcx.sess().source_map().span_to_filename(callee.def_site));
162
197
}
163
198
}
164
199
}
@@ -172,7 +207,6 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
172
207
let mac = MacroRefData :: new ( name. to_string ( ) , callee. def_site , lcx) ;
173
208
self . mac_refs . push ( ( call_site, mac. clone ( ) ) ) ;
174
209
self . collected . insert ( call_site, mac) ;
175
- // println!("TYPE {:?} {:?}", name, lcx.sess().source_map().span_to_filename(callee.def_site));
176
210
}
177
211
}
178
212
}
@@ -181,7 +215,8 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
181
215
fn check_crate_post ( & mut self , lcx : & LateContext < ' _ , ' _ > , _krate : & hir:: Crate < ' _ > ) {
182
216
for ( import, span) in self . imports . iter ( ) {
183
217
184
- let matched = self . mac_refs . iter ( ) . find ( |( _span, mac) | !import. ends_with ( & mac. name ) ) . is_some ( ) ;
218
+ let matched = self . mac_refs . iter ( ) . find ( |( _span, mac) | import. ends_with ( & mac. name ) ) . is_some ( ) ;
219
+
185
220
if matched {
186
221
self . mac_refs . retain ( |( _span, mac) | !import. ends_with ( & mac. name ) ) ;
187
222
let msg = "`macro_use` attributes are no longer needed in the Rust 2018 edition" ;
@@ -197,19 +232,8 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
197
232
)
198
233
}
199
234
}
200
-
201
- for ( span, mac) in self . mac_refs . iter ( ) {
202
- let msg = "`macro_use` attributes are no longer needed in the Rust 2018 edition" ;
203
- let help = make_path ( mac, "hello" ) ;
204
- span_lint_and_sugg (
205
- lcx,
206
- MACRO_USE_IMPORTS ,
207
- * span,
208
- msg,
209
- "remove the attribute and import the macro directly, try" ,
210
- help,
211
- Applicability :: HasPlaceholders ,
212
- )
235
+ if !self . mac_refs . is_empty ( ) {
236
+ println ! ( "{:#?}" , self . mac_refs) ;
213
237
}
214
238
}
215
239
}
0 commit comments