Auto merge of #9053 - AaronC81:fix-9052, r=flip1995

Fix suggestions for `async` closures in redundant_closure_call

Fixes #9052

changelog: Fix suggestions given by [`redundant_closure_call`] for async closures
This commit is contained in:
bors 2022-08-02 12:39:37 +00:00
commit 05e7d5481b
5 changed files with 109 additions and 8 deletions

View file

@ -1,5 +1,5 @@
use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
use clippy_utils::source::snippet_with_applicability;
use clippy_utils::sugg::Sugg;
use if_chain::if_chain;
use rustc_ast::ast;
use rustc_ast::visit as ast_visit;
@ -69,7 +69,7 @@ impl EarlyLintPass for RedundantClosureCall {
if_chain! {
if let ast::ExprKind::Call(ref paren, _) = expr.kind;
if let ast::ExprKind::Paren(ref closure) = paren.kind;
if let ast::ExprKind::Closure(_, _, _, _, ref decl, ref block, _) = closure.kind;
if let ast::ExprKind::Closure(_, _, ref r#async, _, ref decl, ref block, _) = closure.kind;
then {
let mut visitor = ReturnVisitor::new();
visitor.visit_expr(block);
@ -81,10 +81,19 @@ impl EarlyLintPass for RedundantClosureCall {
"try not to call a closure in the expression where it is declared",
|diag| {
if decl.inputs.is_empty() {
let mut app = Applicability::MachineApplicable;
let hint =
snippet_with_applicability(cx, block.span, "..", &mut app).into_owned();
diag.span_suggestion(expr.span, "try doing something like", hint, app);
let app = Applicability::MachineApplicable;
let mut hint = Sugg::ast(cx, block, "..");
if r#async.is_async() {
// `async x` is a syntax error, so it becomes `async { x }`
if !matches!(block.kind, ast::ExprKind::Block(_, _)) {
hint = hint.blockify();
}
hint = hint.asyncify();
}
diag.span_suggestion(expr.span, "try doing something like", hint.to_string(), app);
}
},
);