@@ -2,7 +2,7 @@ use crate::utils::{in_macro, snippet, span_lint_and_sugg};
2
2
use hir:: def:: { DefKind , Res } ;
3
3
use if_chain:: if_chain;
4
4
use rustc_ast:: ast;
5
- use rustc_data_structures:: fx:: FxHashMap ;
5
+ use rustc_data_structures:: fx:: FxHashSet ;
6
6
use rustc_errors:: Applicability ;
7
7
use rustc_hir as hir;
8
8
use rustc_lint:: { LateContext , LateLintPass , LintContext } ;
@@ -42,7 +42,7 @@ impl MacroRefData {
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>
45
- // so remove brackets and space
45
+ // so remove brackets, space and type.
46
46
if path. contains ( '<' ) {
47
47
path = path. replace ( BRACKETS , "" ) ;
48
48
}
@@ -60,10 +60,8 @@ impl MacroRefData {
60
60
pub struct MacroUseImports {
61
61
/// the actual import path used and the span of the attribute above it.
62
62
imports : Vec < ( String , Span ) > ,
63
- /// the span of the macro reference and the `MacroRefData`
64
- /// for the use of the macro.
65
- /// TODO make this FxHashSet<Span> to guard against inserting already found macros
66
- collected : FxHashMap < Span , MacroRefData > ,
63
+ /// the span of the macro reference, kept to ensure only one reference is used per macro call.
64
+ collected : FxHashSet < Span > ,
67
65
mac_refs : Vec < ( Span , MacroRefData ) > ,
68
66
}
69
67
@@ -80,14 +78,9 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
80
78
. find( |attr| attr. ident( ) . map( |s| s. to_string( ) ) == Some ( "macro_use" . to_string( ) ) ) ;
81
79
if let Res :: Def ( DefKind :: Mod , id) = path. res;
82
80
then {
83
- // println!("{:#?}", lcx.tcx.def_path_str(id));
84
81
for kid in lcx. tcx. item_children( id) . iter( ) {
85
- // println!("{:#?}", kid);
86
82
if let Res :: Def ( DefKind :: Macro ( _mac_type) , mac_id) = kid. res {
87
83
let span = mac_attr. span. clone( ) ;
88
-
89
- // println!("{:#?}", lcx.tcx.def_path_str(mac_id));
90
-
91
84
self . imports. push( ( lcx. tcx. def_path_str( mac_id) , span) ) ;
92
85
}
93
86
}
@@ -96,10 +89,9 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
96
89
let call_site = item. span. source_callsite( ) ;
97
90
let name = snippet( lcx, lcx. sess( ) . source_map( ) . span_until_char( call_site, '!' ) , "_" ) ;
98
91
if let Some ( callee) = item. span. source_callee( ) {
99
- if !self . collected. contains_key( & call_site) {
100
- let mac = MacroRefData :: new( name. to_string( ) , callee. def_site, lcx) ;
101
- self . mac_refs. push( ( call_site, mac. clone( ) ) ) ;
102
- self . collected. insert( call_site, mac) ;
92
+ if !self . collected. contains( & call_site) {
93
+ self . mac_refs. push( ( call_site, MacroRefData :: new( name. into( ) , callee. def_site, lcx) ) ) ;
94
+ self . collected. insert( call_site) ;
103
95
}
104
96
}
105
97
}
@@ -111,18 +103,16 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
111
103
let call_site = attr. span . source_callsite ( ) ;
112
104
let name = snippet ( lcx, lcx. sess ( ) . source_map ( ) . span_until_char ( call_site, '!' ) , "_" ) ;
113
105
if let Some ( callee) = attr. span . source_callee ( ) {
114
- if !self . collected . contains_key ( & call_site) {
115
- println ! ( "{:?}\n {:#?}" , call_site, attr) ;
116
-
106
+ if !self . collected . contains ( & call_site) {
117
107
let name = if name. contains ( "::" ) {
118
108
name. split ( "::" ) . last ( ) . unwrap ( ) . to_string ( )
119
109
} else {
120
110
name. to_string ( )
121
111
} ;
122
112
123
- let mac = MacroRefData :: new ( name , callee . def_site , lcx ) ;
124
- self . mac_refs . push ( ( call_site, mac . clone ( ) ) ) ;
125
- self . collected . insert ( call_site, mac ) ;
113
+ self . mac_refs
114
+ . push ( ( call_site, MacroRefData :: new ( name , callee . def_site , lcx ) ) ) ;
115
+ self . collected . insert ( call_site) ;
126
116
}
127
117
}
128
118
}
@@ -132,16 +122,16 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
132
122
let call_site = expr. span . source_callsite ( ) ;
133
123
let name = snippet ( lcx, lcx. sess ( ) . source_map ( ) . span_until_char ( call_site, '!' ) , "_" ) ;
134
124
if let Some ( callee) = expr. span . source_callee ( ) {
135
- if !self . collected . contains_key ( & call_site) {
125
+ if !self . collected . contains ( & call_site) {
136
126
let name = if name. contains ( "::" ) {
137
127
name. split ( "::" ) . last ( ) . unwrap ( ) . to_string ( )
138
128
} else {
139
129
name. to_string ( )
140
130
} ;
141
131
142
- let mac = MacroRefData :: new ( name , callee . def_site , lcx ) ;
143
- self . mac_refs . push ( ( call_site, mac . clone ( ) ) ) ;
144
- self . collected . insert ( call_site, mac ) ;
132
+ self . mac_refs
133
+ . push ( ( call_site, MacroRefData :: new ( name , callee . def_site , lcx ) ) ) ;
134
+ self . collected . insert ( call_site) ;
145
135
}
146
136
}
147
137
}
@@ -151,16 +141,16 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
151
141
let call_site = stmt. span . source_callsite ( ) ;
152
142
let name = snippet ( lcx, lcx. sess ( ) . source_map ( ) . span_until_char ( call_site, '!' ) , "_" ) ;
153
143
if let Some ( callee) = stmt. span . source_callee ( ) {
154
- if !self . collected . contains_key ( & call_site) {
144
+ if !self . collected . contains ( & call_site) {
155
145
let name = if name. contains ( "::" ) {
156
146
name. split ( "::" ) . last ( ) . unwrap ( ) . to_string ( )
157
147
} else {
158
148
name. to_string ( )
159
149
} ;
160
150
161
- let mac = MacroRefData :: new ( name , callee . def_site , lcx ) ;
162
- self . mac_refs . push ( ( call_site, mac . clone ( ) ) ) ;
163
- self . collected . insert ( call_site, mac ) ;
151
+ self . mac_refs
152
+ . push ( ( call_site, MacroRefData :: new ( name , callee . def_site , lcx ) ) ) ;
153
+ self . collected . insert ( call_site) ;
164
154
}
165
155
}
166
156
}
@@ -170,10 +160,10 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
170
160
let call_site = pat. span . source_callsite ( ) ;
171
161
let name = snippet ( lcx, lcx. sess ( ) . source_map ( ) . span_until_char ( call_site, '!' ) , "_" ) ;
172
162
if let Some ( callee) = pat. span . source_callee ( ) {
173
- if !self . collected . contains_key ( & call_site) {
174
- let mac = MacroRefData :: new ( name . to_string ( ) , callee . def_site , lcx ) ;
175
- self . mac_refs . push ( ( call_site, mac . clone ( ) ) ) ;
176
- self . collected . insert ( call_site, mac ) ;
163
+ if !self . collected . contains ( & call_site) {
164
+ self . mac_refs
165
+ . push ( ( call_site, MacroRefData :: new ( name . to_string ( ) , callee . def_site , lcx ) ) ) ;
166
+ self . collected . insert ( call_site) ;
177
167
}
178
168
}
179
169
}
@@ -183,10 +173,10 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
183
173
let call_site = ty. span . source_callsite ( ) ;
184
174
let name = snippet ( lcx, lcx. sess ( ) . source_map ( ) . span_until_char ( call_site, '!' ) , "_" ) ;
185
175
if let Some ( callee) = ty. span . source_callee ( ) {
186
- if !self . collected . contains_key ( & call_site) {
187
- let mac = MacroRefData :: new ( name . to_string ( ) , callee . def_site , lcx ) ;
188
- self . mac_refs . push ( ( call_site, mac . clone ( ) ) ) ;
189
- self . collected . insert ( call_site, mac ) ;
176
+ if !self . collected . contains ( & call_site) {
177
+ self . mac_refs
178
+ . push ( ( call_site, MacroRefData :: new ( name . to_string ( ) , callee . def_site , lcx ) ) ) ;
179
+ self . collected . insert ( call_site) ;
190
180
}
191
181
}
192
182
}
0 commit comments