@@ -29,7 +29,7 @@ declare_clippy_lint! {
29
29
30
30
const BRACKETS : & [ char ] = & [ '<' , '>' ] ;
31
31
32
- /// MacroRefData includes the name of the macro
32
+ /// ` MacroRefData` includes the name of the macro
33
33
/// and the path from `SourceMap::span_to_filename`.
34
34
#[ derive( Debug , Clone ) ]
35
35
pub struct MacroRefData {
@@ -38,7 +38,7 @@ pub struct MacroRefData {
38
38
}
39
39
40
40
impl MacroRefData {
41
- pub fn new ( name : String , span : Span , ecx : & LateContext < ' _ , ' _ > ) -> Self {
41
+ pub fn new ( name : & str , span : Span , ecx : & LateContext < ' _ , ' _ > ) -> Self {
42
42
let mut path = ecx. sess ( ) . source_map ( ) . span_to_filename ( span) . to_string ( ) ;
43
43
44
44
// std lib paths are <::std::module::file type>
@@ -57,6 +57,7 @@ impl MacroRefData {
57
57
}
58
58
59
59
#[ derive( Default ) ]
60
+ #[ allow( clippy:: module_name_repetitions) ]
60
61
pub struct MacroUseImports {
61
62
/// the actual import path used and the span of the attribute above it.
62
63
imports : Vec < ( String , Span ) > ,
@@ -70,27 +71,27 @@ impl_lint_pass!(MacroUseImports => [MACRO_USE_IMPORTS]);
70
71
impl < ' l , ' txc > LateLintPass < ' l , ' txc > for MacroUseImports {
71
72
fn check_item ( & mut self , lcx : & LateContext < ' _ , ' _ > , item : & hir:: Item < ' _ > ) {
72
73
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
+ }
85
87
}
86
- }
87
- } else {
88
+ } else {
88
89
if in_macro( item. span) {
89
90
let call_site = item. span. source_callsite( ) ;
90
91
let name = snippet( lcx, lcx. sess( ) . source_map( ) . span_until_char( call_site, '!' ) , "_" ) ;
91
92
if let Some ( callee) = item. span. source_callee( ) {
92
93
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) ) ) ;
94
95
self . collected. insert( call_site) ;
95
96
}
96
97
}
@@ -111,7 +112,7 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
111
112
} ;
112
113
113
114
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) ) ) ;
115
116
self . collected . insert ( call_site) ;
116
117
}
117
118
}
@@ -130,7 +131,7 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
130
131
} ;
131
132
132
133
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) ) ) ;
134
135
self . collected . insert ( call_site) ;
135
136
}
136
137
}
@@ -149,7 +150,7 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
149
150
} ;
150
151
151
152
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) ) ) ;
153
154
self . collected . insert ( call_site) ;
154
155
}
155
156
}
@@ -162,7 +163,7 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
162
163
if let Some ( callee) = pat. span . source_callee ( ) {
163
164
if !self . collected . contains ( & call_site) {
164
165
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) ) ) ;
166
167
self . collected . insert ( call_site) ;
167
168
}
168
169
}
@@ -175,20 +176,16 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
175
176
if let Some ( callee) = ty. span . source_callee ( ) {
176
177
if !self . collected . contains ( & call_site) {
177
178
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) ) ) ;
179
180
self . collected . insert ( call_site) ;
180
181
}
181
182
}
182
183
}
183
184
}
184
185
185
186
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 ) ) ;
192
189
193
190
if matched {
194
191
self . mac_refs . retain ( |( _span, mac) | !import. ends_with ( & mac. name ) ) ;
@@ -208,30 +205,8 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
208
205
if !self . mac_refs . is_empty ( ) {
209
206
// TODO if not empty we found one we could not make a suggestion for
210
207
// 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?
212
210
}
213
211
}
214
212
}
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