Skip to content

Commit 5d59792

Browse files
committed
remove stdout, fix clippy warnings, fmtcar
1 parent 57d20ac commit 5d59792

File tree

5 files changed

+76
-69
lines changed

5 files changed

+76
-69
lines changed

clippy_lints/src/macro_use.rs

+27-52
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ declare_clippy_lint! {
2929

3030
const BRACKETS: &[char] = &['<', '>'];
3131

32-
/// MacroRefData includes the name of the macro
32+
/// `MacroRefData` includes the name of the macro
3333
/// and the path from `SourceMap::span_to_filename`.
3434
#[derive(Debug, Clone)]
3535
pub struct MacroRefData {
@@ -38,7 +38,7 @@ pub struct MacroRefData {
3838
}
3939

4040
impl MacroRefData {
41-
pub fn new(name: String, span: Span, ecx: &LateContext<'_, '_>) -> Self {
41+
pub fn new(name: &str, span: Span, ecx: &LateContext<'_, '_>) -> Self {
4242
let mut path = ecx.sess().source_map().span_to_filename(span).to_string();
4343

4444
// std lib paths are <::std::module::file type>
@@ -57,6 +57,7 @@ impl MacroRefData {
5757
}
5858

5959
#[derive(Default)]
60+
#[allow(clippy::module_name_repetitions)]
6061
pub struct MacroUseImports {
6162
/// the actual import path used and the span of the attribute above it.
6263
imports: Vec<(String, Span)>,
@@ -70,27 +71,27 @@ impl_lint_pass!(MacroUseImports => [MACRO_USE_IMPORTS]);
7071
impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
7172
fn check_item(&mut self, lcx: &LateContext<'_, '_>, item: &hir::Item<'_>) {
7273
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+
}
8587
}
86-
}
87-
} else {
88+
} else {
8889
if in_macro(item.span) {
8990
let call_site = item.span.source_callsite();
9091
let name = snippet(lcx, lcx.sess().source_map().span_until_char(call_site, '!'), "_");
9192
if let Some(callee) = item.span.source_callee() {
9293
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)));
9495
self.collected.insert(call_site);
9596
}
9697
}
@@ -111,7 +112,7 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
111112
};
112113

113114
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)));
115116
self.collected.insert(call_site);
116117
}
117118
}
@@ -130,7 +131,7 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
130131
};
131132

132133
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)));
134135
self.collected.insert(call_site);
135136
}
136137
}
@@ -149,7 +150,7 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
149150
};
150151

151152
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)));
153154
self.collected.insert(call_site);
154155
}
155156
}
@@ -162,7 +163,7 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
162163
if let Some(callee) = pat.span.source_callee() {
163164
if !self.collected.contains(&call_site) {
164165
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)));
166167
self.collected.insert(call_site);
167168
}
168169
}
@@ -175,20 +176,16 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
175176
if let Some(callee) = ty.span.source_callee() {
176177
if !self.collected.contains(&call_site) {
177178
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)));
179180
self.collected.insert(call_site);
180181
}
181182
}
182183
}
183184
}
184185

185186
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));
192189

193190
if matched {
194191
self.mac_refs.retain(|(_span, mac)| !import.ends_with(&mac.name));
@@ -208,30 +205,8 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
208205
if !self.mac_refs.is_empty() {
209206
// TODO if not empty we found one we could not make a suggestion for
210207
// 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?
212210
}
213211
}
214212
}
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-
}

tests/ui/auxiliary/macro_use_helper.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub mod inner {
1717

1818
// ITEM
1919
#[macro_export]
20-
macro_rules! inner_mod {
20+
macro_rules! inner_mod_macro {
2121
() => {
2222
#[allow(dead_code)]
2323
pub struct Tardis;
@@ -27,7 +27,7 @@ pub mod inner {
2727

2828
// EXPR
2929
#[macro_export]
30-
macro_rules! function {
30+
macro_rules! function_macro {
3131
() => {
3232
if true {
3333
} else {
@@ -37,7 +37,7 @@ macro_rules! function {
3737

3838
// TYPE
3939
#[macro_export]
40-
macro_rules! ty_mac {
40+
macro_rules! ty_macro {
4141
() => {
4242
Vec<u8>
4343
};
@@ -46,7 +46,7 @@ macro_rules! ty_mac {
4646
mod extern_exports {
4747
pub(super) mod private_inner {
4848
#[macro_export]
49-
macro_rules! pub_in_private {
49+
macro_rules! pub_in_private_macro {
5050
($name:ident) => {
5151
let $name = String::from("secrets and lies");
5252
};

tests/ui/macro_use_import.stdout

Whitespace-only changes.

tests/ui/macro_use_imports.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ extern crate macro_use_helper as mac;
1212
extern crate clippy_mini_macro_test as mini_mac;
1313

1414
mod a {
15-
#[macro_use]
16-
use std::prelude;
1715
#[macro_use]
1816
use mac;
1917
#[macro_use]
@@ -26,15 +24,13 @@ mod a {
2624

2725
fn main() {
2826
pub_macro!();
29-
inner_mod!();
30-
pub_in_private!(_var);
31-
function!();
32-
let v: ty_mac!() = Vec::default();
27+
inner_mod_macro!();
28+
pub_in_private_macro!(_var);
29+
function_macro!();
30+
let v: ty_macro!() = Vec::default();
3331

3432
inner::try_err!();
3533
}
3634
}
3735

38-
fn main() {
39-
println!();
40-
}
36+
fn main() {}

tests/ui/macro_use_imports.stderr

+40-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,46 @@
11
error: `macro_use` attributes are no longer needed in the Rust 2018 edition
2-
--> $DIR/macro_use_imports.rs:5:1
2+
--> $DIR/macro_use_imports.rs:15:5
33
|
4-
LL | #[macro_use]
5-
| ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use std::prelude::<macro name>`
4+
LL | #[macro_use]
5+
| ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mac::pub_macro`
66
|
77
= note: `-D clippy::macro-use-imports` implied by `-D warnings`
88

9-
error: aborting due to previous error
9+
error: `macro_use` attributes are no longer needed in the Rust 2018 edition
10+
--> $DIR/macro_use_imports.rs:15:5
11+
|
12+
LL | #[macro_use]
13+
| ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mac::inner_mod_macro`
14+
15+
error: `macro_use` attributes are no longer needed in the Rust 2018 edition
16+
--> $DIR/macro_use_imports.rs:15:5
17+
|
18+
LL | #[macro_use]
19+
| ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mac::function_macro`
20+
21+
error: `macro_use` attributes are no longer needed in the Rust 2018 edition
22+
--> $DIR/macro_use_imports.rs:15:5
23+
|
24+
LL | #[macro_use]
25+
| ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mac::ty_macro`
26+
27+
error: `macro_use` attributes are no longer needed in the Rust 2018 edition
28+
--> $DIR/macro_use_imports.rs:15:5
29+
|
30+
LL | #[macro_use]
31+
| ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mac::pub_in_private_macro`
32+
33+
error: `macro_use` attributes are no longer needed in the Rust 2018 edition
34+
--> $DIR/macro_use_imports.rs:17:5
35+
|
36+
LL | #[macro_use]
37+
| ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mini_mac::ClippyMiniMacroTest`
38+
39+
error: `macro_use` attributes are no longer needed in the Rust 2018 edition
40+
--> $DIR/macro_use_imports.rs:19:5
41+
|
42+
LL | #[macro_use]
43+
| ^^^^^^^^^^^^ help: remove the attribute and import the macro directly, try: `use mac::inner::try_err`
44+
45+
error: aborting due to 7 previous errors
1046

0 commit comments

Comments
 (0)