Skip to content

Commit 5deffa6

Browse files
committed
clean upish
1 parent b74796d commit 5deffa6

File tree

1 file changed

+40
-42
lines changed

1 file changed

+40
-42
lines changed

clippy_lints/src/macro_use.rs

+40-42
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,25 @@ const PRELUDE: &[&str] = &[
1313
"macros"
1414
];
1515
const BRACKETS: &[char] = &['<', '>'];
16+
17+
declare_clippy_lint! {
18+
/// **What it does:** Checks for `#[macro_use] use...`.
19+
///
20+
/// **Why is this bad?** Since the Rust 2018 edition you can import
21+
/// macro's directly, this is considered idiomatic.
22+
///
23+
/// **Known problems:** None.
24+
///
25+
/// **Example:**
26+
/// ```rust
27+
/// #[macro_use]
28+
/// use lazy_static;
29+
/// ```
30+
pub MACRO_USE_IMPORTS,
31+
pedantic,
32+
"#[macro_use] is no longer needed"
33+
}
34+
1635
/// MacroRefData includes the name of the macro
1736
/// and the path from `SourceMap::span_to_filename`.
1837
pub struct MacroRefData {
@@ -32,29 +51,10 @@ impl MacroRefData {
3251
if path.contains(' ') {
3352
path = path.split(' ').next().unwrap().to_string();
3453
}
35-
println!("NEW {} {}", name, path);
3654
Self { name: name.to_string(), path, }
3755
}
3856
}
3957

40-
declare_clippy_lint! {
41-
/// **What it does:** Checks for `#[macro_use] use...`.
42-
///
43-
/// **Why is this bad?** Since the Rust 2018 edition you can import
44-
/// macro's directly, this is considered idiomatic.
45-
///
46-
/// **Known problems:** None.
47-
///
48-
/// **Example:**
49-
/// ```rust
50-
/// #[macro_use]
51-
/// use lazy_static;
52-
/// ```
53-
pub MACRO_USE_IMPORTS,
54-
pedantic,
55-
"#[macro_use] is no longer needed"
56-
}
57-
5858
#[derive(Default)]
5959
pub struct MacroUseImports {
6060
/// the actual import path used and its span.
@@ -66,8 +66,6 @@ pub struct MacroUseImports {
6666

6767
impl MacroUseImports {
6868
fn import_path_mac(&self, use_path: &str) -> String {
69-
println!("END {:?}", use_path);
70-
7169
for mac in self.collected.values() {
7270
if paths_match(mac, use_path) {
7371
return make_path(mac, use_path)
@@ -82,8 +80,6 @@ fn paths_match(mac: &MacroRefData, use_path: &str) -> bool {
8280
.filter(|s| *s != "")
8381
.collect::<Vec<_>>();
8482

85-
println!("{:?}", segs);
86-
8783
if segs.starts_with(&["std"]) {
8884
return PRELUDE.iter().any(|m| segs.contains(m))
8985
}
@@ -92,14 +88,19 @@ fn paths_match(mac: &MacroRefData, use_path: &str) -> bool {
9288
}
9389

9490
fn make_path(mac: &MacroRefData, use_path: &str) -> String {
95-
let mut res = String::default();
96-
91+
if use_path.split("::").count() == 1 {
92+
return format!("{}::{}", use_path, mac.name);
93+
}
94+
9795
let segs = mac.path.split("::")
98-
.filter(|s| *s == "")
96+
.filter(|s| *s != "")
9997
.collect::<Vec<_>>();
10098

99+
if segs.starts_with(&["std"]) && PRELUDE.iter().any(|m| segs.contains(m)) {
100+
return format!("std::prelude::{}", mac.name);
101+
}
101102

102-
res
103+
mac.path.clone()
103104
}
104105

105106
impl_lint_pass!(MacroUseImports => [MACRO_USE_IMPORTS]);
@@ -128,7 +129,6 @@ impl EarlyLintPass for MacroUseImports {
128129
if let Some(callee) = expr.span.source_callee() {
129130
self.collected.entry(call_site)
130131
.or_insert_with(|| {
131-
println!("EXPR {:?}", name);
132132
MacroRefData::new(name.to_string(), callee.def_site, ecx)
133133
});
134134
}
@@ -141,7 +141,6 @@ impl EarlyLintPass for MacroUseImports {
141141
if let Some(callee) = stmt.span.source_callee() {
142142
self.collected.entry(call_site)
143143
.or_insert_with(|| {
144-
println!("STMT {:?}", name);
145144
MacroRefData::new(name.to_string(), callee.def_site, ecx)
146145
});
147146
}
@@ -154,25 +153,25 @@ impl EarlyLintPass for MacroUseImports {
154153
if let Some(callee) = pat.span.source_callee() {
155154
self.collected.entry(call_site)
156155
.or_insert_with(|| {
157-
println!("PAT {:?}", name);
158156
MacroRefData::new(name.to_string(), callee.def_site, ecx)
159157
});
160158
}
161159
}
162160
}
163-
fn check_mac(&mut self, ecx: &EarlyContext<'_>, mac: &ast::Mac) {
164-
let call_site = mac.span().source_callsite();
165-
let name = snippet(ecx, ecx.sess.source_map().span_until_char(call_site, '!'), "_");
166-
if let Some(callee) = mac.span().source_callee() {
167-
self.collected.entry(call_site)
168-
.or_insert_with(|| {
169-
println!("MAC {:?}", name);
170-
MacroRefData::new(name.to_string(), callee.def_site, ecx)
171-
});
161+
fn check_ty(&mut self, ecx: &EarlyContext<'_>, ty: &ast::Ty) {
162+
if in_macro(ty.span) {
163+
let call_site = ty.span.source_callsite();
164+
let name = snippet(ecx, ecx.sess.source_map().span_until_char(call_site, '!'), "_");
165+
if let Some(callee) = ty.span.source_callee() {
166+
self.collected.entry(call_site)
167+
.or_insert_with(|| {
168+
MacroRefData::new(name.to_string(), callee.def_site, ecx)
169+
});
170+
}
172171
}
173172
}
174173

175-
fn check_crate_post(&mut self, ecx: &EarlyContext<'_>, krate: &ast::Crate) {
174+
fn check_crate_post(&mut self, ecx: &EarlyContext<'_>, _krate: &ast::Crate) {
176175
for (name, span) in self.imports.iter() {
177176
let import_path = self.import_path_mac(&name);
178177
let msg = "`macro_use` attributes are no longer needed in the Rust 2018 edition";
@@ -182,8 +181,7 @@ impl EarlyLintPass for MacroUseImports {
182181
MACRO_USE_IMPORTS,
183182
*span,
184183
msg,
185-
// "remove the attribute and import the macro directly, try",
186-
"",
184+
"remove the attribute and import the macro directly, try",
187185
help,
188186
Applicability::HasPlaceholders,
189187
)

0 commit comments

Comments
 (0)