1
1
use clippy_utils:: diagnostics:: { span_lint, span_lint_and_then} ;
2
- use clippy_utils:: source :: snippet_with_applicability ;
2
+ use clippy_utils:: sugg :: Sugg ;
3
3
use if_chain:: if_chain;
4
4
use rustc_ast:: ast;
5
5
use rustc_ast:: visit as ast_visit;
@@ -69,7 +69,7 @@ impl EarlyLintPass for RedundantClosureCall {
69
69
if_chain ! {
70
70
if let ast:: ExprKind :: Call ( ref paren, _) = expr. kind;
71
71
if let ast:: ExprKind :: Paren ( ref closure) = paren. kind;
72
- if let ast:: ExprKind :: Closure ( _, _, _ , _, ref decl, ref block, _) = closure. kind;
72
+ if let ast:: ExprKind :: Closure ( _, _, ref r#async , _, ref decl, ref block, _) = closure. kind;
73
73
then {
74
74
let mut visitor = ReturnVisitor :: new( ) ;
75
75
visitor. visit_expr( block) ;
@@ -81,10 +81,19 @@ impl EarlyLintPass for RedundantClosureCall {
81
81
"try not to call a closure in the expression where it is declared" ,
82
82
|diag| {
83
83
if decl. inputs. is_empty( ) {
84
- let mut app = Applicability :: MachineApplicable ;
85
- let hint =
86
- snippet_with_applicability( cx, block. span, ".." , & mut app) . into_owned( ) ;
87
- diag. span_suggestion( expr. span, "try doing something like" , hint, app) ;
84
+ let app = Applicability :: MachineApplicable ;
85
+ let mut hint = Sugg :: ast( cx, block, ".." ) ;
86
+
87
+ if r#async. is_async( ) {
88
+ // `async x` is a syntax error, so it becomes `async { x }`
89
+ if !matches!( block. kind, ast:: ExprKind :: Block ( _, _) ) {
90
+ hint = hint. blockify( ) ;
91
+ }
92
+
93
+ hint = hint. asyncify( ) ;
94
+ }
95
+
96
+ diag. span_suggestion( expr. span, "try doing something like" , hint. to_string( ) , app) ;
88
97
}
89
98
} ,
90
99
) ;
0 commit comments