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...`.
11
12
///
12
13
/// **Why is this bad?** Since the Rust 2018 edition you can import
13
14
/// macro's directly, this is considered idiomatic.
14
15
///
15
- /// **Known problems:** This lint does not generate an auto-applicable suggestion .
16
+ /// **Known problems:** None .
16
17
///
17
18
/// **Example:**
18
19
/// ```rust
19
20
/// #[macro_use]
20
21
/// use lazy_static;
21
22
/// ```
22
- pub MACRO_USE_IMPORTS ,
23
+ pub MACRO_USE_IMPORT ,
23
24
pedantic,
24
25
"#[macro_use] is no longer needed"
25
26
}
26
27
27
- declare_lint_pass ! ( MacroUseImports => [ MACRO_USE_IMPORTS ] ) ;
28
+ #[ derive( Default ) ]
29
+ pub struct MacroUseImport {
30
+ collected : FxHashSet < Span > ,
31
+ }
32
+
33
+ impl_lint_pass ! ( MacroUseImport => [ MACRO_USE_IMPORT ] ) ;
34
+
35
+ impl EarlyLintPass for MacroUseImport {
28
36
29
- impl EarlyLintPass for MacroUseImports {
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 ;
@@ -36,18 +43,59 @@ impl EarlyLintPass for MacroUseImports {
36
43
. iter( )
37
44
. find( |attr| attr. ident( ) . map( |s| s. to_string( ) ) == Some ( "macro_use" . to_string( ) ) ) ;
38
45
then {
46
+ let import_path = snippet( ecx, use_tree. span, "_" ) ;
47
+ let mac_names = find_used_macros( ecx, & import_path) ;
39
48
let msg = "`macro_use` attributes are no longer needed in the Rust 2018 edition" ;
40
- let help = format!( "use {}::<macro name>" , snippet ( ecx , use_tree . span , "_" ) ) ;
49
+ let help = format!( "use {}::<macro name>" , import_path ) ;
41
50
span_lint_and_sugg(
42
51
ecx,
43
- MACRO_USE_IMPORTS ,
52
+ MACRO_USE_IMPORT ,
44
53
mac_attr. span,
45
54
msg,
46
- "remove the attribute and import the macro directly, try" ,
55
+ // "remove the attribute and import the macro directly, try",
56
+ "" ,
47
57
help,
48
58
Applicability :: HasPlaceholders ,
49
59
) ;
50
60
}
51
61
}
52
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
+ }
90
+ }
91
+
92
+ fn find_used_macros ( ecx : & EarlyContext < ' _ > , path : & str ) {
93
+ for it in ecx. krate . module . items . iter ( ) {
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);
100
+ }
53
101
}
0 commit comments