@@ -9,11 +9,6 @@ use rustc_session::{declare_tool_lint, impl_lint_pass};
9
9
use rustc_span:: { edition:: Edition , Span } ;
10
10
use rustc_ast:: ast;
11
11
12
- const PRELUDE : & [ & str ] = & [
13
- "marker" , "ops" , "convert" , "iter" , "option" , "result" , "borrow" , "boxed" , "string" , "vec" , "macros" ,
14
- ] ;
15
- const BRACKETS : & [ char ] = & [ '<' , '>' ] ;
16
-
17
12
declare_clippy_lint ! {
18
13
/// **What it does:** Checks for `#[macro_use] use...`.
19
14
///
@@ -32,6 +27,8 @@ declare_clippy_lint! {
32
27
"#[macro_use] is no longer needed"
33
28
}
34
29
30
+ const BRACKETS : & [ char ] = & [ '<' , '>' ] ;
31
+
35
32
/// MacroRefData includes the name of the macro
36
33
/// and the path from `SourceMap::span_to_filename`.
37
34
#[ derive( Debug , Clone ) ]
@@ -61,30 +58,15 @@ impl MacroRefData {
61
58
62
59
#[ derive( Default ) ]
63
60
pub struct MacroUseImports {
64
- /// the actual import path used and its span.
61
+ /// the actual import path used and the span of the attribute above it .
65
62
imports : Vec < ( String , Span ) > ,
66
63
/// the span of the macro reference and the `MacroRefData`
67
64
/// for the use of the macro.
65
+ /// TODO make this FxHashSet<Span> to guard against inserting already found macros
68
66
collected : FxHashMap < Span , MacroRefData > ,
69
67
mac_refs : Vec < ( Span , MacroRefData ) > ,
70
68
}
71
69
72
- /// This is somewhat of a fallback for imports from `std::prelude` because they
73
- /// are not recognized by `LateLintPass::check_item` `lcx.tcx.item_children(id)`
74
- fn make_path ( mac : & MacroRefData , use_path : & str ) -> String {
75
- let segs = mac. path . split ( "::" ) . filter ( |s| * s != "" ) . collect :: < Vec < _ > > ( ) ;
76
-
77
- if segs. starts_with ( & [ "std" ] ) && PRELUDE . iter ( ) . any ( |m| segs. contains ( m) ) {
78
- return format ! ( "std::prelude::{} is imported by default, remove `use` statement" , mac. name) ;
79
- }
80
-
81
- if use_path. split ( "::" ) . count ( ) == 1 {
82
- return format ! ( "{}::{}" , use_path, mac. name) ;
83
- }
84
-
85
- mac. path . clone ( )
86
- }
87
-
88
70
impl_lint_pass ! ( MacroUseImports => [ MACRO_USE_IMPORTS ] ) ;
89
71
90
72
impl < ' l , ' txc > LateLintPass < ' l , ' txc > for MacroUseImports {
@@ -233,9 +215,30 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
233
215
}
234
216
}
235
217
if !self . mac_refs . is_empty ( ) {
236
- // TODO if not empty we found one would could not make a suggestion for
218
+ // TODO if not empty we found one we could not make a suggestion for
237
219
// such as std::prelude::v1 or something else I haven't thought of.
238
220
// println!("{:#?}", self.mac_refs);
239
221
}
240
222
}
241
223
}
224
+
225
+
226
+ const PRELUDE : & [ & str ] = & [
227
+ "marker" , "ops" , "convert" , "iter" , "option" , "result" , "borrow" , "boxed" , "string" , "vec" , "macros" ,
228
+ ] ;
229
+
230
+ /// This is somewhat of a fallback for imports from `std::prelude` because they
231
+ /// are not recognized by `LateLintPass::check_item` `lcx.tcx.item_children(id)`
232
+ fn make_path ( mac : & MacroRefData , use_path : & str ) -> String {
233
+ let segs = mac. path . split ( "::" ) . filter ( |s| * s != "" ) . collect :: < Vec < _ > > ( ) ;
234
+
235
+ if segs. starts_with ( & [ "std" ] ) && PRELUDE . iter ( ) . any ( |m| segs. contains ( m) ) {
236
+ return format ! ( "std::prelude::{} is imported by default, remove `use` statement" , mac. name) ;
237
+ }
238
+
239
+ if use_path. split ( "::" ) . count ( ) == 1 {
240
+ return format ! ( "{}::{}" , use_path, mac. name) ;
241
+ }
242
+
243
+ mac. path . clone ( )
244
+ }
0 commit comments