@@ -7,6 +7,7 @@ use hir::def::{Res, DefKind};
77use rustc_lint:: { LintContext , LateLintPass , LateContext } ;
88use rustc_session:: { declare_tool_lint, impl_lint_pass} ;
99use rustc_span:: { edition:: Edition , Span } ;
10+ use rustc_ast:: ast;
1011
1112const PRELUDE : & [ & str ] = & [
1213 "marker" , "ops" , "convert" , "iter" , "option" , "result" , "borrow" , "boxed" , "string" , "vec" , "macros" ,
@@ -97,10 +98,14 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
9798 . find( |attr| attr. ident( ) . map( |s| s. to_string( ) ) == Some ( "macro_use" . to_string( ) ) ) ;
9899 if let Res :: Def ( DefKind :: Mod , id) = path. res;
99100 then {
101+ // println!("{:#?}", lcx.tcx.def_path_str(id));
100102 for kid in lcx. tcx. item_children( id) . iter( ) {
103+ // println!("{:#?}", kid);
101104 if let Res :: Def ( DefKind :: Macro ( _mac_type) , mac_id) = kid. res {
102105 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+
104109 self . imports. push( ( lcx. tcx. def_path_str( mac_id) , span) ) ;
105110 }
106111 }
@@ -113,24 +118,49 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
113118 let mac = MacroRefData :: new( name. to_string( ) , callee. def_site, lcx) ;
114119 self . mac_refs. push( ( call_site, mac. clone( ) ) ) ;
115120 self . collected. insert( call_site, mac) ;
116- // println!("EXPR {:?} {:?}", name, lcx.sess().source_map().span_to_filename(callee.def_site));
117121 }
118122 }
119123 }
120124 }
121125 }
122126 }
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+ } ;
123140
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+ }
124148 fn check_expr ( & mut self , lcx : & LateContext < ' _ , ' _ > , expr : & hir:: Expr < ' _ > ) {
125149 if in_macro ( expr. span ) {
126150 let call_site = expr. span . source_callsite ( ) ;
127151 let name = snippet ( lcx, lcx. sess ( ) . source_map ( ) . span_until_char ( call_site, '!' ) , "_" ) ;
128152 if let Some ( callee) = expr. span . source_callee ( ) {
129153 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) ;
131162 self . mac_refs . push ( ( call_site, mac. clone ( ) ) ) ;
132163 self . collected . insert ( call_site, mac) ;
133- // println!("EXPR {:?} {:?}", name, lcx.sess().source_map().span_to_filename(callee.def_site));
134164 }
135165 }
136166 }
@@ -141,10 +171,16 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
141171 let name = snippet ( lcx, lcx. sess ( ) . source_map ( ) . span_until_char ( call_site, '!' ) , "_" ) ;
142172 if let Some ( callee) = stmt. span . source_callee ( ) {
143173 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) ;
145182 self . mac_refs . push ( ( call_site, mac. clone ( ) ) ) ;
146183 self . collected . insert ( call_site, mac) ;
147- // println!("STMT {:?} {:?}", name, lcx.sess().source_map().span_to_filename(callee.def_site));
148184 }
149185 }
150186 }
@@ -158,7 +194,6 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
158194 let mac = MacroRefData :: new ( name. to_string ( ) , callee. def_site , lcx) ;
159195 self . mac_refs . push ( ( call_site, mac. clone ( ) ) ) ;
160196 self . collected . insert ( call_site, mac) ;
161- // println!("PAT {:?} {:?}", name, lcx.sess().source_map().span_to_filename(callee.def_site));
162197 }
163198 }
164199 }
@@ -172,7 +207,6 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
172207 let mac = MacroRefData :: new ( name. to_string ( ) , callee. def_site , lcx) ;
173208 self . mac_refs . push ( ( call_site, mac. clone ( ) ) ) ;
174209 self . collected . insert ( call_site, mac) ;
175- // println!("TYPE {:?} {:?}", name, lcx.sess().source_map().span_to_filename(callee.def_site));
176210 }
177211 }
178212 }
@@ -181,7 +215,8 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
181215 fn check_crate_post ( & mut self , lcx : & LateContext < ' _ , ' _ > , _krate : & hir:: Crate < ' _ > ) {
182216 for ( import, span) in self . imports . iter ( ) {
183217
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+
185220 if matched {
186221 self . mac_refs . retain ( |( _span, mac) | !import. ends_with ( & mac. name ) ) ;
187222 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 {
197232 )
198233 }
199234 }
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) ;
213237 }
214238 }
215239}
0 commit comments