@@ -29,7 +29,7 @@ declare_clippy_lint! {
2929
3030const BRACKETS : & [ char ] = & [ '<' , '>' ] ;
3131
32- /// MacroRefData includes the name of the macro
32+ /// ` MacroRefData` includes the name of the macro
3333/// and the path from `SourceMap::span_to_filename`.
3434#[ derive( Debug , Clone ) ]
3535pub struct MacroRefData {
@@ -38,7 +38,7 @@ pub struct MacroRefData {
3838}
3939
4040impl MacroRefData {
41- pub fn new ( name : String , span : Span , ecx : & LateContext < ' _ , ' _ > ) -> Self {
41+ pub fn new ( name : & str , span : Span , ecx : & LateContext < ' _ , ' _ > ) -> Self {
4242 let mut path = ecx. sess ( ) . source_map ( ) . span_to_filename ( span) . to_string ( ) ;
4343
4444 // std lib paths are <::std::module::file type>
@@ -57,6 +57,7 @@ impl MacroRefData {
5757}
5858
5959#[ derive( Default ) ]
60+ #[ allow( clippy:: module_name_repetitions) ]
6061pub struct MacroUseImports {
6162 /// the actual import path used and the span of the attribute above it.
6263 imports : Vec < ( String , Span ) > ,
@@ -70,27 +71,27 @@ impl_lint_pass!(MacroUseImports => [MACRO_USE_IMPORTS]);
7071impl < ' l , ' txc > LateLintPass < ' l , ' txc > for MacroUseImports {
7172 fn check_item ( & mut self , lcx : & LateContext < ' _ , ' _ > , item : & hir:: Item < ' _ > ) {
7273 if_chain ! {
73- if lcx. sess( ) . opts. edition == Edition :: Edition2018 ;
74- if let hir:: ItemKind :: Use ( path, _kind) = & item. kind;
75- if let Some ( mac_attr) = item
76- . attrs
77- . iter( )
78- . find( |attr| attr. ident( ) . map( |s| s. to_string( ) ) == Some ( "macro_use" . to_string( ) ) ) ;
79- if let Res :: Def ( DefKind :: Mod , id) = path. res;
80- then {
81- for kid in lcx. tcx. item_children( id) . iter( ) {
82- if let Res :: Def ( DefKind :: Macro ( _mac_type) , mac_id) = kid. res {
83- let span = mac_attr. span. clone( ) ;
84- self . imports. push( ( lcx. tcx. def_path_str( mac_id) , span) ) ;
74+ if lcx. sess( ) . opts. edition == Edition :: Edition2018 ;
75+ if let hir:: ItemKind :: Use ( path, _kind) = & item. kind;
76+ if let Some ( mac_attr) = item
77+ . attrs
78+ . iter( )
79+ . find( |attr| attr. ident( ) . map( |s| s. to_string( ) ) == Some ( "macro_use" . to_string( ) ) ) ;
80+ if let Res :: Def ( DefKind :: Mod , id) = path. res;
81+ then {
82+ for kid in lcx. tcx. item_children( id) . iter( ) {
83+ if let Res :: Def ( DefKind :: Macro ( _mac_type) , mac_id) = kid. res {
84+ let span = mac_attr. span;
85+ self . imports. push( ( lcx. tcx. def_path_str( mac_id) , span) ) ;
86+ }
8587 }
86- }
87- } else {
88+ } else {
8889 if in_macro( item. span) {
8990 let call_site = item. span. source_callsite( ) ;
9091 let name = snippet( lcx, lcx. sess( ) . source_map( ) . span_until_char( call_site, '!' ) , "_" ) ;
9192 if let Some ( callee) = item. span. source_callee( ) {
9293 if !self . collected. contains( & call_site) {
93- self . mac_refs. push( ( call_site, MacroRefData :: new( name. into ( ) , callee. def_site, lcx) ) ) ;
94+ self . mac_refs. push( ( call_site, MacroRefData :: new( & name, callee. def_site, lcx) ) ) ;
9495 self . collected. insert( call_site) ;
9596 }
9697 }
@@ -111,7 +112,7 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
111112 } ;
112113
113114 self . mac_refs
114- . push ( ( call_site, MacroRefData :: new ( name, callee. def_site , lcx) ) ) ;
115+ . push ( ( call_site, MacroRefData :: new ( & name, callee. def_site , lcx) ) ) ;
115116 self . collected . insert ( call_site) ;
116117 }
117118 }
@@ -130,7 +131,7 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
130131 } ;
131132
132133 self . mac_refs
133- . push ( ( call_site, MacroRefData :: new ( name, callee. def_site , lcx) ) ) ;
134+ . push ( ( call_site, MacroRefData :: new ( & name, callee. def_site , lcx) ) ) ;
134135 self . collected . insert ( call_site) ;
135136 }
136137 }
@@ -149,7 +150,7 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
149150 } ;
150151
151152 self . mac_refs
152- . push ( ( call_site, MacroRefData :: new ( name, callee. def_site , lcx) ) ) ;
153+ . push ( ( call_site, MacroRefData :: new ( & name, callee. def_site , lcx) ) ) ;
153154 self . collected . insert ( call_site) ;
154155 }
155156 }
@@ -162,7 +163,7 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
162163 if let Some ( callee) = pat. span . source_callee ( ) {
163164 if !self . collected . contains ( & call_site) {
164165 self . mac_refs
165- . push ( ( call_site, MacroRefData :: new ( name. to_string ( ) , callee. def_site , lcx) ) ) ;
166+ . push ( ( call_site, MacroRefData :: new ( & name, callee. def_site , lcx) ) ) ;
166167 self . collected . insert ( call_site) ;
167168 }
168169 }
@@ -175,20 +176,16 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
175176 if let Some ( callee) = ty. span . source_callee ( ) {
176177 if !self . collected . contains ( & call_site) {
177178 self . mac_refs
178- . push ( ( call_site, MacroRefData :: new ( name. to_string ( ) , callee. def_site , lcx) ) ) ;
179+ . push ( ( call_site, MacroRefData :: new ( & name, callee. def_site , lcx) ) ) ;
179180 self . collected . insert ( call_site) ;
180181 }
181182 }
182183 }
183184 }
184185
185186 fn check_crate_post ( & mut self , lcx : & LateContext < ' _ , ' _ > , _krate : & hir:: Crate < ' _ > ) {
186- for ( import, span) in self . imports . iter ( ) {
187- let matched = self
188- . mac_refs
189- . iter ( )
190- . find ( |( _span, mac) | import. ends_with ( & mac. name ) )
191- . is_some ( ) ;
187+ for ( import, span) in & self . imports {
188+ let matched = self . mac_refs . iter ( ) . any ( |( _span, mac) | import. ends_with ( & mac. name ) ) ;
192189
193190 if matched {
194191 self . mac_refs . retain ( |( _span, mac) | !import. ends_with ( & mac. name ) ) ;
@@ -208,30 +205,8 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
208205 if !self . mac_refs . is_empty ( ) {
209206 // TODO if not empty we found one we could not make a suggestion for
210207 // such as std::prelude::v1 or something else I haven't thought of.
211- // println!("{:#?}", self.mac_refs);
208+ // If we defer the calling of span_lint_and_sugg we can make a decision about its
209+ // applicability?
212210 }
213211 }
214212}
215-
216- const PRELUDE : & [ & str ] = & [
217- "marker" , "ops" , "convert" , "iter" , "option" , "result" , "borrow" , "boxed" , "string" , "vec" , "macros" ,
218- ] ;
219-
220- /// This is somewhat of a fallback for imports from `std::prelude` because they
221- /// are not recognized by `LateLintPass::check_item` `lcx.tcx.item_children(id)`
222- fn make_path ( mac : & MacroRefData , use_path : & str ) -> String {
223- let segs = mac. path . split ( "::" ) . filter ( |s| * s != "" ) . collect :: < Vec < _ > > ( ) ;
224-
225- if segs. starts_with ( & [ "std" ] ) && PRELUDE . iter ( ) . any ( |m| segs. contains ( m) ) {
226- return format ! (
227- "std::prelude::{} is imported by default, remove `use` statement" ,
228- mac. name
229- ) ;
230- }
231-
232- if use_path. split ( "::" ) . count ( ) == 1 {
233- return format ! ( "{}::{}" , use_path, mac. name) ;
234- }
235-
236- mac. path . clone ( )
237- }
0 commit comments