11use clippy_utils:: diagnostics:: { span_lint, span_lint_and_then} ;
2- use clippy_utils:: source :: snippet_with_applicability ;
2+ use clippy_utils:: sugg :: Sugg ;
33use if_chain:: if_chain;
44use rustc_ast:: ast;
55use rustc_ast:: visit as ast_visit;
@@ -69,7 +69,7 @@ impl EarlyLintPass for RedundantClosureCall {
6969 if_chain ! {
7070 if let ast:: ExprKind :: Call ( ref paren, _) = expr. kind;
7171 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;
7373 then {
7474 let mut visitor = ReturnVisitor :: new( ) ;
7575 visitor. visit_expr( block) ;
@@ -81,10 +81,19 @@ impl EarlyLintPass for RedundantClosureCall {
8181 "try not to call a closure in the expression where it is declared" ,
8282 |diag| {
8383 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) ;
8897 }
8998 } ,
9099 ) ;
0 commit comments