Auto merge of #11363 - KisaragiEffective:fix_redundant_closure_call_on_closure_returns_async_block, r=llogiq

[`redundant_closure_call`]: avoid duplicated `async` keyword when triggering on closure that returns `async` block

close #11357

----

*Please write a short comment explaining your change (or "none" for internal only changes)*

changelog: [`redundant_closure_call`]: avoid duplicated `async` keyword when triggering on closure that returns `async` block
This commit is contained in:
bors 2023-11-28 20:27:48 +00:00
commit 57397a5190
4 changed files with 77 additions and 14 deletions

View file

@ -4,8 +4,8 @@ use clippy_utils::get_parent_expr;
use clippy_utils::sugg::Sugg; use clippy_utils::sugg::Sugg;
use rustc_errors::Applicability; use rustc_errors::Applicability;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::intravisit as hir_visit;
use rustc_hir::intravisit::{Visitor as HirVisitor, Visitor}; use rustc_hir::intravisit::{Visitor as HirVisitor, Visitor};
use rustc_hir::{intravisit as hir_visit, CoroutineKind, CoroutineSource, Node};
use rustc_lint::{LateContext, LateLintPass}; use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::hir::nested_filter; use rustc_middle::hir::nested_filter;
use rustc_middle::lint::in_external_macro; use rustc_middle::lint::in_external_macro;
@ -60,11 +60,14 @@ impl<'tcx> Visitor<'tcx> for ReturnVisitor {
} }
} }
/// Checks if the body is owned by an async closure /// Checks if the body is owned by an async closure.
fn is_async_closure(body: &hir::Body<'_>) -> bool { /// Returns true for `async || whatever_expression`, but false for `|| async { whatever_expression
if let hir::ExprKind::Closure(closure) = body.value.kind /// }`.
&& let [resume_ty] = closure.fn_decl.inputs fn is_async_closure(cx: &LateContext<'_>, body: &hir::Body<'_>) -> bool {
&& let hir::TyKind::Path(hir::QPath::LangItem(hir::LangItem::ResumeTy, ..)) = resume_ty.kind if let hir::ExprKind::Closure(innermost_closure_generated_by_desugar) = body.value.kind
&& let desugared_inner_closure_body = cx.tcx.hir().body(innermost_closure_generated_by_desugar.body)
// checks whether it is `async || whatever_expression`
&& let Some(CoroutineKind::Async(CoroutineSource::Closure)) = desugared_inner_closure_body.coroutine_kind
{ {
true true
} else { } else {
@ -100,7 +103,7 @@ fn find_innermost_closure<'tcx>(
data = Some(( data = Some((
body.value, body.value,
closure.fn_decl, closure.fn_decl,
if is_async_closure(body) { if is_async_closure(cx, body) {
ty::Asyncness::Yes ty::Asyncness::Yes
} else { } else {
ty::Asyncness::No ty::Asyncness::No
@ -173,12 +176,18 @@ impl<'tcx> LateLintPass<'tcx> for RedundantClosureCall {
hint = hint.asyncify(); hint = hint.asyncify();
} }
diag.span_suggestion( let is_in_fn_call_arg =
full_expr.span, clippy_utils::get_parent_node(cx.tcx, expr.hir_id).is_some_and(|x| match x {
"try doing something like", Node::Expr(expr) => matches!(expr.kind, hir::ExprKind::Call(_, _)),
hint.maybe_par(), _ => false,
applicability, });
);
// avoid clippy::double_parens
if !is_in_fn_call_arg {
hint = hint.maybe_par();
};
diag.span_suggestion(full_expr.span, "try doing something like", hint, applicability);
} }
}, },
); );

View file

@ -84,3 +84,21 @@ fn issue9956() {
bar()(42, 5); bar()(42, 5);
foo(42, 5); foo(42, 5);
} }
async fn issue11357() {
async {}.await;
}
mod issue11707 {
use core::future::Future;
fn spawn_on(fut: impl Future<Output = ()>) {}
fn demo() {
spawn_on(async move {});
}
}
fn avoid_double_parens() {
std::convert::identity(13_i32 + 36_i32).leading_zeros();
}

View file

@ -84,3 +84,21 @@ fn issue9956() {
bar()((|| || 42)()(), 5); bar()((|| || 42)()(), 5);
foo((|| || 42)()(), 5); foo((|| || 42)()(), 5);
} }
async fn issue11357() {
(|| async {})().await;
}
mod issue11707 {
use core::future::Future;
fn spawn_on(fut: impl Future<Output = ()>) {}
fn demo() {
spawn_on((|| async move {})());
}
}
fn avoid_double_parens() {
std::convert::identity((|| 13_i32 + 36_i32)()).leading_zeros();
}

View file

@ -123,5 +123,23 @@ error: try not to call a closure in the expression where it is declared
LL | foo((|| || 42)()(), 5); LL | foo((|| || 42)()(), 5);
| ^^^^^^^^^^^^^^ help: try doing something like: `42` | ^^^^^^^^^^^^^^ help: try doing something like: `42`
error: aborting due to 14 previous errors error: try not to call a closure in the expression where it is declared
--> $DIR/redundant_closure_call_fixable.rs:89:5
|
LL | (|| async {})().await;
| ^^^^^^^^^^^^^^^ help: try doing something like: `async {}`
error: try not to call a closure in the expression where it is declared
--> $DIR/redundant_closure_call_fixable.rs:98:18
|
LL | spawn_on((|| async move {})());
| ^^^^^^^^^^^^^^^^^^^^ help: try doing something like: `async move {}`
error: try not to call a closure in the expression where it is declared
--> $DIR/redundant_closure_call_fixable.rs:103:28
|
LL | std::convert::identity((|| 13_i32 + 36_i32)()).leading_zeros();
| ^^^^^^^^^^^^^^^^^^^^^^ help: try doing something like: `13_i32 + 36_i32`
error: aborting due to 17 previous errors