Skip to content

Commit 42dcbe9

Browse files
committed
add more imports to test
1 parent 58b4930 commit 42dcbe9

File tree

4 files changed

+93
-38
lines changed

4 files changed

+93
-38
lines changed

clippy_lints/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1023,7 +1023,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
10231023
store.register_early_pass(|| box option_env_unwrap::OptionEnvUnwrap);
10241024
store.register_late_pass(|| box wildcard_imports::WildcardImports);
10251025
store.register_late_pass(|| box verbose_file_reads::VerboseFileReads);
1026-
// store.register_early_pass(|| box macro_use::MacroUseImports::default());
10271026
store.register_late_pass(|| box macro_use::MacroUseImports::default());
10281027

10291028
store.register_group(true, "clippy::restriction", Some("clippy_restriction"), vec![

clippy_lints/src/macro_use.rs

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use hir::def::{Res, DefKind};
77
use rustc_lint::{LintContext, LateLintPass, LateContext};
88
use rustc_session::{declare_tool_lint, impl_lint_pass};
99
use rustc_span::{edition::Edition, Span};
10+
use rustc_ast::ast;
1011

1112
const PRELUDE: &[&str] = &[
1213
"marker", "ops", "convert", "iter", "option", "result", "borrow", "boxed", "string", "vec", "macros",
@@ -97,10 +98,14 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
9798
.find(|attr| attr.ident().map(|s| s.to_string()) == Some("macro_use".to_string()));
9899
if let Res::Def(DefKind::Mod, id) = path.res;
99100
then {
101+
// println!("{:#?}", lcx.tcx.def_path_str(id));
100102
for kid in lcx.tcx.item_children(id).iter() {
103+
// println!("{:#?}", kid);
101104
if let Res::Def(DefKind::Macro(_mac_type), mac_id) = kid.res {
102105
let span = mac_attr.span.clone();
103-
println!("{:#?}", lcx.tcx.def_path_str(mac_id));
106+
107+
// println!("{:#?}", lcx.tcx.def_path_str(mac_id));
108+
104109
self.imports.push((lcx.tcx.def_path_str(mac_id), span));
105110
}
106111
}
@@ -113,24 +118,49 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
113118
let mac = MacroRefData::new(name.to_string(), callee.def_site, lcx);
114119
self.mac_refs.push((call_site, mac.clone()));
115120
self.collected.insert(call_site, mac);
116-
// println!("EXPR {:?} {:?}", name, lcx.sess().source_map().span_to_filename(callee.def_site));
117121
}
118122
}
119123
}
120124
}
121125
}
122126
}
127+
fn check_attribute(&mut self, lcx: &LateContext<'_, '_>, attr: &ast::Attribute) {
128+
if in_macro(attr.span) {
129+
let call_site = attr.span.source_callsite();
130+
let name = snippet(lcx, lcx.sess().source_map().span_until_char(call_site, '!'), "_");
131+
if let Some(callee) = attr.span.source_callee() {
132+
if !self.collected.contains_key(&call_site) {
133+
println!("{:?}\n{:#?}", call_site, attr);
134+
135+
let name = if name.contains("::") {
136+
name.split("::").last().unwrap().to_string()
137+
} else {
138+
name.to_string()
139+
};
123140

141+
let mac = MacroRefData::new(name, callee.def_site, lcx);
142+
self.mac_refs.push((call_site, mac.clone()));
143+
self.collected.insert(call_site, mac);
144+
}
145+
}
146+
}
147+
}
124148
fn check_expr(&mut self, lcx: &LateContext<'_, '_>, expr: &hir::Expr<'_>) {
125149
if in_macro(expr.span) {
126150
let call_site = expr.span.source_callsite();
127151
let name = snippet(lcx, lcx.sess().source_map().span_until_char(call_site, '!'), "_");
128152
if let Some(callee) = expr.span.source_callee() {
129153
if !self.collected.contains_key(&call_site) {
130-
let mac = MacroRefData::new(name.to_string(), callee.def_site, lcx);
154+
155+
let name = if name.contains("::") {
156+
name.split("::").last().unwrap().to_string()
157+
} else {
158+
name.to_string()
159+
};
160+
161+
let mac = MacroRefData::new(name, callee.def_site, lcx);
131162
self.mac_refs.push((call_site, mac.clone()));
132163
self.collected.insert(call_site, mac);
133-
// println!("EXPR {:?} {:?}", name, lcx.sess().source_map().span_to_filename(callee.def_site));
134164
}
135165
}
136166
}
@@ -141,10 +171,16 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
141171
let name = snippet(lcx, lcx.sess().source_map().span_until_char(call_site, '!'), "_");
142172
if let Some(callee) = stmt.span.source_callee() {
143173
if !self.collected.contains_key(&call_site) {
144-
let mac = MacroRefData::new(name.to_string(), callee.def_site, lcx);
174+
175+
let name = if name.contains("::") {
176+
name.split("::").last().unwrap().to_string()
177+
} else {
178+
name.to_string()
179+
};
180+
181+
let mac = MacroRefData::new(name, callee.def_site, lcx);
145182
self.mac_refs.push((call_site, mac.clone()));
146183
self.collected.insert(call_site, mac);
147-
// println!("STMT {:?} {:?}", name, lcx.sess().source_map().span_to_filename(callee.def_site));
148184
}
149185
}
150186
}
@@ -158,7 +194,6 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
158194
let mac = MacroRefData::new(name.to_string(), callee.def_site, lcx);
159195
self.mac_refs.push((call_site, mac.clone()));
160196
self.collected.insert(call_site, mac);
161-
// println!("PAT {:?} {:?}", name, lcx.sess().source_map().span_to_filename(callee.def_site));
162197
}
163198
}
164199
}
@@ -172,7 +207,6 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
172207
let mac = MacroRefData::new(name.to_string(), callee.def_site, lcx);
173208
self.mac_refs.push((call_site, mac.clone()));
174209
self.collected.insert(call_site, mac);
175-
// println!("TYPE {:?} {:?}", name, lcx.sess().source_map().span_to_filename(callee.def_site));
176210
}
177211
}
178212
}
@@ -181,7 +215,8 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
181215
fn check_crate_post(&mut self, lcx: &LateContext<'_, '_>, _krate: &hir::Crate<'_>) {
182216
for (import, span) in self.imports.iter() {
183217

184-
let matched = self.mac_refs.iter().find(|(_span, mac)| !import.ends_with(&mac.name)).is_some();
218+
let matched = self.mac_refs.iter().find(|(_span, mac)| import.ends_with(&mac.name)).is_some();
219+
185220
if matched {
186221
self.mac_refs.retain(|(_span, mac)| !import.ends_with(&mac.name));
187222
let msg = "`macro_use` attributes are no longer needed in the Rust 2018 edition";
@@ -197,19 +232,8 @@ impl<'l, 'txc> LateLintPass<'l, 'txc> for MacroUseImports {
197232
)
198233
}
199234
}
200-
201-
for (span, mac) in self.mac_refs.iter() {
202-
let msg = "`macro_use` attributes are no longer needed in the Rust 2018 edition";
203-
let help = make_path(mac, "hello");
204-
span_lint_and_sugg(
205-
lcx,
206-
MACRO_USE_IMPORTS,
207-
*span,
208-
msg,
209-
"remove the attribute and import the macro directly, try",
210-
help,
211-
Applicability::HasPlaceholders,
212-
)
235+
if !self.mac_refs.is_empty() {
236+
println!("{:#?}", self.mac_refs);
213237
}
214238
}
215239
}

tests/ui/auxiliary/macro_use_helper.rs

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// pub use macro_rules::foofoo;
2-
// pub use crate::extern_exports::*;
1+
extern crate macro_rules;
32

3+
// STMT
44
#[macro_export]
55
macro_rules! pub_macro {
66
() => {
@@ -10,17 +10,39 @@ macro_rules! pub_macro {
1010

1111

1212
pub mod inner {
13+
pub use super::*;
14+
15+
// RE-EXPORT
16+
// this will stick in `inner` module
17+
pub use macro_rules::try_err;
18+
19+
// ITEM
1320
#[macro_export]
14-
macro_rules! inner_mod {
15-
() => {
16-
#[allow(dead_code)]
17-
pub struct Tardis;
18-
};
19-
}
21+
macro_rules! inner_mod {
22+
() => {
23+
#[allow(dead_code)]
24+
pub struct Tardis;
25+
};
26+
}
27+
}
28+
29+
// EXPR
30+
#[macro_export]
31+
macro_rules! function {
32+
() => {
33+
if true {} else {}
34+
};
35+
}
36+
37+
// TYPE
38+
#[macro_export]
39+
macro_rules! ty_mac {
40+
() => {
41+
Vec<u8>
42+
};
2043
}
2144

2245
mod extern_exports {
23-
2446
pub(super) mod private_inner {
2547
#[macro_export]
2648
macro_rules! pub_in_private {

tests/ui/macro_use_imports.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,37 @@
11
// compile-flags: --edition 2018
2+
// aux-build:macro_rules.rs
23
// aux-build:macro_use_helper.rs
34

4-
// aux-build:macro_rules.rs
55
#![allow(clippy::single_component_path_imports)]
66
#![warn(clippy::macro_use_imports)]
7-
#![feature(rustc_private)]
87

98
#[macro_use]
109
extern crate macro_use_helper as mac;
10+
1111
#[macro_use]
12-
use std::prelude;
12+
extern crate clippy_mini_macro_test as mini_mac;
1313

1414
mod a {
15-
use std::collections::HashMap;
15+
#[macro_use]
16+
use std::prelude;
1617
#[macro_use]
1718
use mac;
19+
#[macro_use]
20+
use mini_mac;
21+
#[macro_use]
22+
use mac::inner;
23+
24+
#[derive(ClippyMiniMacroTest)]
25+
struct Test;
1826

1927
fn main() {
20-
let _ = HashMap::<u8, u8>::new();
21-
println!();
2228
pub_macro!();
2329
inner_mod!();
2430
pub_in_private!(_var);
31+
function!();
32+
let v: ty_mac!() = Vec::default();
33+
34+
inner::try_err!();
2535
}
2636
}
2737

0 commit comments

Comments
 (0)