Skip to content

Commit e864f01

Browse files
committed
use hashset not map for keeping track of seen macro refs
1 parent 6e69981 commit e864f01

File tree

1 file changed

+27
-37
lines changed

1 file changed

+27
-37
lines changed

clippy_lints/src/macro_use.rs

+27-37
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::utils::{in_macro, snippet, span_lint_and_sugg};
22
use hir::def::{DefKind, Res};
33
use if_chain::if_chain;
44
use rustc_ast::ast;
5-
use rustc_data_structures::fx::FxHashMap;
5+
use rustc_data_structures::fx::FxHashSet;
66
use rustc_errors::Applicability;
77
use rustc_hir as hir;
88
use rustc_lint::{LateContext, LateLintPass, LintContext};
@@ -42,7 +42,7 @@ impl MacroRefData {
4242
let mut path = ecx.sess().source_map().span_to_filename(span).to_string();
4343

4444
// std lib paths are <::std::module::file type>
45-
// so remove brackets and space
45+
// so remove brackets, space and type.
4646
if path.contains('<') {
4747
path = path.replace(BRACKETS, "");
4848
}
@@ -60,10 +60,8 @@ impl MacroRefData {
6060
pub struct MacroUseImports {
6161
/// the actual import path used and the span of the attribute above it.
6262
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>,
6765
mac_refs: Vec<(Span, MacroRefData)>,
6866
}
6967

@@ -80,14 +78,9 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
8078
.find(|attr| attr.ident().map(|s| s.to_string()) == Some("macro_use".to_string()));
8179
if let Res::Def(DefKind::Mod, id) = path.res;
8280
then {
83-
// println!("{:#?}", lcx.tcx.def_path_str(id));
8481
for kid in lcx.tcx.item_children(id).iter() {
85-
// println!("{:#?}", kid);
8682
if let Res::Def(DefKind::Macro(_mac_type), mac_id) = kid.res {
8783
let span = mac_attr.span.clone();
88-
89-
// println!("{:#?}", lcx.tcx.def_path_str(mac_id));
90-
9184
self.imports.push((lcx.tcx.def_path_str(mac_id), span));
9285
}
9386
}
@@ -96,10 +89,9 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
9689
let call_site = item.span.source_callsite();
9790
let name = snippet(lcx, lcx.sess().source_map().span_until_char(call_site, '!'), "_");
9891
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);
10395
}
10496
}
10597
}
@@ -111,18 +103,16 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
111103
let call_site = attr.span.source_callsite();
112104
let name = snippet(lcx, lcx.sess().source_map().span_until_char(call_site, '!'), "_");
113105
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) {
117107
let name = if name.contains("::") {
118108
name.split("::").last().unwrap().to_string()
119109
} else {
120110
name.to_string()
121111
};
122112

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);
126116
}
127117
}
128118
}
@@ -132,16 +122,16 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
132122
let call_site = expr.span.source_callsite();
133123
let name = snippet(lcx, lcx.sess().source_map().span_until_char(call_site, '!'), "_");
134124
if let Some(callee) = expr.span.source_callee() {
135-
if !self.collected.contains_key(&call_site) {
125+
if !self.collected.contains(&call_site) {
136126
let name = if name.contains("::") {
137127
name.split("::").last().unwrap().to_string()
138128
} else {
139129
name.to_string()
140130
};
141131

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);
145135
}
146136
}
147137
}
@@ -151,16 +141,16 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
151141
let call_site = stmt.span.source_callsite();
152142
let name = snippet(lcx, lcx.sess().source_map().span_until_char(call_site, '!'), "_");
153143
if let Some(callee) = stmt.span.source_callee() {
154-
if !self.collected.contains_key(&call_site) {
144+
if !self.collected.contains(&call_site) {
155145
let name = if name.contains("::") {
156146
name.split("::").last().unwrap().to_string()
157147
} else {
158148
name.to_string()
159149
};
160150

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);
164154
}
165155
}
166156
}
@@ -170,10 +160,10 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
170160
let call_site = pat.span.source_callsite();
171161
let name = snippet(lcx, lcx.sess().source_map().span_until_char(call_site, '!'), "_");
172162
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);
177167
}
178168
}
179169
}
@@ -183,10 +173,10 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
183173
let call_site = ty.span.source_callsite();
184174
let name = snippet(lcx, lcx.sess().source_map().span_until_char(call_site, '!'), "_");
185175
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);
190180
}
191181
}
192182
}

0 commit comments

Comments
 (0)