@@ -13,6 +13,25 @@ const PRELUDE: &[&str] = &[
13
13
"macros"
14
14
] ;
15
15
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
+
16
35
/// MacroRefData includes the name of the macro
17
36
/// and the path from `SourceMap::span_to_filename`.
18
37
pub struct MacroRefData {
@@ -32,29 +51,10 @@ impl MacroRefData {
32
51
if path. contains ( ' ' ) {
33
52
path = path. split ( ' ' ) . next ( ) . unwrap ( ) . to_string ( ) ;
34
53
}
35
- println ! ( "NEW {} {}" , name, path) ;
36
54
Self { name : name. to_string ( ) , path, }
37
55
}
38
56
}
39
57
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
-
58
58
#[ derive( Default ) ]
59
59
pub struct MacroUseImports {
60
60
/// the actual import path used and its span.
@@ -66,8 +66,6 @@ pub struct MacroUseImports {
66
66
67
67
impl MacroUseImports {
68
68
fn import_path_mac ( & self , use_path : & str ) -> String {
69
- println ! ( "END {:?}" , use_path) ;
70
-
71
69
for mac in self . collected . values ( ) {
72
70
if paths_match ( mac, use_path) {
73
71
return make_path ( mac, use_path)
@@ -82,8 +80,6 @@ fn paths_match(mac: &MacroRefData, use_path: &str) -> bool {
82
80
. filter ( |s| * s != "" )
83
81
. collect :: < Vec < _ > > ( ) ;
84
82
85
- println ! ( "{:?}" , segs) ;
86
-
87
83
if segs. starts_with ( & [ "std" ] ) {
88
84
return PRELUDE . iter ( ) . any ( |m| segs. contains ( m) )
89
85
}
@@ -92,14 +88,19 @@ fn paths_match(mac: &MacroRefData, use_path: &str) -> bool {
92
88
}
93
89
94
90
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
+
97
95
let segs = mac. path . split ( "::" )
98
- . filter ( |s| * s = = "" )
96
+ . filter ( |s| * s ! = "" )
99
97
. collect :: < Vec < _ > > ( ) ;
100
98
99
+ if segs. starts_with ( & [ "std" ] ) && PRELUDE . iter ( ) . any ( |m| segs. contains ( m) ) {
100
+ return format ! ( "std::prelude::{}" , mac. name) ;
101
+ }
101
102
102
- res
103
+ mac . path . clone ( )
103
104
}
104
105
105
106
impl_lint_pass ! ( MacroUseImports => [ MACRO_USE_IMPORTS ] ) ;
@@ -128,7 +129,6 @@ impl EarlyLintPass for MacroUseImports {
128
129
if let Some ( callee) = expr. span . source_callee ( ) {
129
130
self . collected . entry ( call_site)
130
131
. or_insert_with ( || {
131
- println ! ( "EXPR {:?}" , name) ;
132
132
MacroRefData :: new ( name. to_string ( ) , callee. def_site , ecx)
133
133
} ) ;
134
134
}
@@ -141,7 +141,6 @@ impl EarlyLintPass for MacroUseImports {
141
141
if let Some ( callee) = stmt. span . source_callee ( ) {
142
142
self . collected . entry ( call_site)
143
143
. or_insert_with ( || {
144
- println ! ( "STMT {:?}" , name) ;
145
144
MacroRefData :: new ( name. to_string ( ) , callee. def_site , ecx)
146
145
} ) ;
147
146
}
@@ -154,25 +153,25 @@ impl EarlyLintPass for MacroUseImports {
154
153
if let Some ( callee) = pat. span . source_callee ( ) {
155
154
self . collected . entry ( call_site)
156
155
. or_insert_with ( || {
157
- println ! ( "PAT {:?}" , name) ;
158
156
MacroRefData :: new ( name. to_string ( ) , callee. def_site , ecx)
159
157
} ) ;
160
158
}
161
159
}
162
160
}
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
+ }
172
171
}
173
172
}
174
173
175
- fn check_crate_post ( & mut self , ecx : & EarlyContext < ' _ > , krate : & ast:: Crate ) {
174
+ fn check_crate_post ( & mut self , ecx : & EarlyContext < ' _ > , _krate : & ast:: Crate ) {
176
175
for ( name, span) in self . imports . iter ( ) {
177
176
let import_path = self . import_path_mac ( & name) ;
178
177
let msg = "`macro_use` attributes are no longer needed in the Rust 2018 edition" ;
@@ -182,8 +181,7 @@ impl EarlyLintPass for MacroUseImports {
182
181
MACRO_USE_IMPORTS ,
183
182
* span,
184
183
msg,
185
- // "remove the attribute and import the macro directly, try",
186
- "" ,
184
+ "remove the attribute and import the macro directly, try" ,
187
185
help,
188
186
Applicability :: HasPlaceholders ,
189
187
)
0 commit comments