1- use crate :: utils:: { snippet, span_lint_and_sugg} ;
1+ use crate :: utils:: { snippet, span_lint_and_sugg, in_macro } ;
22use if_chain:: if_chain;
33use rustc_ast:: ast;
4+ use rustc_data_structures:: fx:: FxHashSet ;
45use rustc_errors:: Applicability ;
56use 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 } ;
89
910declare_clippy_lint ! {
1011 /// **What it does:** Checks for `#[macro_use] use...`.
@@ -24,9 +25,15 @@ declare_clippy_lint! {
2425 "#[macro_use] is no longer needed"
2526}
2627
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 ] ) ;
2834
2935impl EarlyLintPass for MacroUseImport {
36+
3037 fn check_item ( & mut self , ecx : & EarlyContext < ' _ > , item : & ast:: Item ) {
3138 if_chain ! {
3239 if ecx. sess. opts. edition == Edition :: Edition2018 ;
@@ -45,17 +52,50 @@ impl EarlyLintPass for MacroUseImport {
4552 MACRO_USE_IMPORT ,
4653 mac_attr. span,
4754 msg,
48- "remove the attribute and import the macro directly, try" ,
55+ // "remove the attribute and import the macro directly, try",
56+ "" ,
4957 help,
5058 Applicability :: HasPlaceholders ,
5159 ) ;
5260 }
5361 }
5462 }
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+ }
5590}
5691
5792fn find_used_macros ( ecx : & EarlyContext < ' _ > , path : & str ) {
5893 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);
60100 }
61101}
0 commit comments