1
- use crate :: utils:: { snippet, span_lint_and_sugg} ;
1
+ use crate :: utils:: { snippet, span_lint_and_sugg, in_macro } ;
2
2
use if_chain:: if_chain;
3
3
use rustc_ast:: ast;
4
+ use rustc_data_structures:: fx:: FxHashSet ;
4
5
use rustc_errors:: Applicability ;
5
6
use rustc_lint:: { EarlyContext , EarlyLintPass } ;
6
- use rustc_session:: { declare_lint_pass , declare_tool_lint} ;
7
- use rustc_span:: edition:: Edition ;
7
+ use rustc_session:: { impl_lint_pass , declare_tool_lint} ;
8
+ use rustc_span:: { edition:: Edition , Span } ;
8
9
9
10
declare_clippy_lint ! {
10
11
/// **What it does:** Checks for `#[macro_use] use...`.
@@ -24,9 +25,15 @@ declare_clippy_lint! {
24
25
"#[macro_use] is no longer needed"
25
26
}
26
27
27
- declare_lint_pass ! ( MacroUseImport => [ MACRO_USE_IMPORT ] ) ;
28
+ #[ derive( Default ) ]
29
+ pub struct MacroUseImport {
30
+ collected : FxHashSet < Span > ,
31
+ }
32
+
33
+ impl_lint_pass ! ( MacroUseImport => [ MACRO_USE_IMPORT ] ) ;
28
34
29
35
impl EarlyLintPass for MacroUseImport {
36
+
30
37
fn check_item ( & mut self , ecx : & EarlyContext < ' _ > , item : & ast:: Item ) {
31
38
if_chain ! {
32
39
if ecx. sess. opts. edition == Edition :: Edition2018 ;
@@ -45,17 +52,50 @@ impl EarlyLintPass for MacroUseImport {
45
52
MACRO_USE_IMPORT ,
46
53
mac_attr. span,
47
54
msg,
48
- "remove the attribute and import the macro directly, try" ,
55
+ // "remove the attribute and import the macro directly, try",
56
+ "" ,
49
57
help,
50
58
Applicability :: HasPlaceholders ,
51
59
) ;
52
60
}
53
61
}
54
62
}
63
+
64
+ fn check_expr ( & mut self , ecx : & EarlyContext < ' _ > , expr : & ast:: Expr ) {
65
+ if in_macro ( expr. span ) {
66
+ let name = snippet ( ecx, ecx. sess . source_map ( ) . span_until_char ( expr. span . source_callsite ( ) , '!' ) , "_" ) ;
67
+ if let Some ( callee) = expr. span . source_callee ( ) {
68
+ if self . collected . insert ( callee. def_site ) {
69
+ println ! ( "EXPR {:#?}" , name) ;
70
+ }
71
+ }
72
+ }
73
+ }
74
+ fn check_stmt ( & mut self , ecx : & EarlyContext < ' _ > , stmt : & ast:: Stmt ) {
75
+ if in_macro ( stmt. span ) {
76
+ let name = snippet ( ecx, ecx. sess . source_map ( ) . span_until_char ( stmt. span . source_callsite ( ) , '!' ) , "_" ) ;
77
+ if let Some ( callee) = stmt. span . source_callee ( ) {
78
+ println ! ( "EXPR {:#?}" , name) ;
79
+ }
80
+ }
81
+ }
82
+ fn check_pat ( & mut self , ecx : & EarlyContext < ' _ > , pat : & ast:: Pat ) {
83
+ if in_macro ( pat. span ) {
84
+ let name = snippet ( ecx, ecx. sess . source_map ( ) . span_until_char ( pat. span . source_callsite ( ) , '!' ) , "_" ) ;
85
+ if let Some ( callee) = pat. span . source_callee ( ) {
86
+ println ! ( "EXPR {:#?}" , name) ;
87
+ }
88
+ }
89
+ }
55
90
}
56
91
57
92
fn find_used_macros ( ecx : & EarlyContext < ' _ > , path : & str ) {
58
93
for it in ecx. krate . module . items . iter ( ) {
59
- println ! ( "{:#?}" , it) ;
94
+ if in_macro ( it. span ) {
95
+ // println!("{:#?}", it)
96
+ }
97
+ }
98
+ for x in ecx. sess . imported_macro_spans . borrow ( ) . iter ( ) {
99
+ // println!("{:?}", x);
60
100
}
61
101
}
0 commit comments