Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 1661e7e

Browse files
re-implement fix for rust-lang#11357
1 parent 5ba6480 commit 1661e7e

4 files changed

+67
-22
lines changed

clippy_lints/src/redundant_closure_call.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use clippy_utils::get_parent_expr;
44
use clippy_utils::sugg::Sugg;
55
use rustc_errors::Applicability;
66
use rustc_hir as hir;
7-
use rustc_hir::intravisit as hir_visit;
87
use rustc_hir::intravisit::{Visitor as HirVisitor, Visitor};
8+
use rustc_hir::{intravisit as hir_visit, CoroutineKind, CoroutineSource};
99
use rustc_lint::{LateContext, LateLintPass};
1010
use rustc_middle::hir::nested_filter;
1111
use rustc_middle::lint::in_external_macro;
@@ -60,11 +60,14 @@ impl<'tcx> Visitor<'tcx> for ReturnVisitor {
6060
}
6161
}
6262

63-
/// Checks if the body is owned by an async closure
64-
fn is_async_closure(body: &hir::Body<'_>) -> bool {
65-
if let hir::ExprKind::Closure(closure) = body.value.kind
66-
&& let [resume_ty] = closure.fn_decl.inputs
67-
&& let hir::TyKind::Path(hir::QPath::LangItem(hir::LangItem::ResumeTy, ..)) = resume_ty.kind
63+
/// Checks if the body is owned by an async closure.
64+
/// Returns true for `async || whatever_expression`, but false for `|| async { whatever_expression
65+
/// }`.
66+
fn is_async_closure(cx: &LateContext<'_>, body: &hir::Body<'_>) -> bool {
67+
if let hir::ExprKind::Closure(innermost_closure_generated_by_desugar) = body.value.kind
68+
&& let desugared_inner_closure_body = cx.tcx.hir().body(innermost_closure_generated_by_desugar.body)
69+
// checks whether it is `async || whatever_expression`
70+
&& let Some(CoroutineKind::Async(CoroutineSource::Closure)) = desugared_inner_closure_body.coroutine_kind
6871
{
6972
true
7073
} else {
@@ -100,7 +103,7 @@ fn find_innermost_closure<'tcx>(
100103
data = Some((
101104
body.value,
102105
closure.fn_decl,
103-
if is_async_closure(body) {
106+
if is_async_closure(cx, body) {
104107
ty::Asyncness::Yes
105108
} else {
106109
ty::Asyncness::No

tests/ui/redundant_closure_call_fixable.fixed

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#![allow(clippy::redundant_async_block)]
44
#![allow(clippy::type_complexity)]
55
#![allow(unused)]
6+
#![allow(clippy::double_parens)]
67

78
async fn something() -> u32 {
89
21
@@ -84,3 +85,17 @@ fn issue9956() {
8485
bar()(42, 5);
8586
foo(42, 5);
8687
}
88+
89+
async fn issue11357() {
90+
(async {}).await;
91+
}
92+
93+
mod issue11707 {
94+
use core::future::Future;
95+
96+
fn spawn_on(fut: impl Future<Output = ()>) {}
97+
98+
fn demo() {
99+
spawn_on((async move {}));
100+
}
101+
}

tests/ui/redundant_closure_call_fixable.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#![allow(clippy::redundant_async_block)]
44
#![allow(clippy::type_complexity)]
55
#![allow(unused)]
6+
#![allow(clippy::double_parens)]
67

78
async fn something() -> u32 {
89
21
@@ -84,3 +85,17 @@ fn issue9956() {
8485
bar()((|| || 42)()(), 5);
8586
foo((|| || 42)()(), 5);
8687
}
88+
89+
async fn issue11357() {
90+
(|| async {})().await;
91+
}
92+
93+
mod issue11707 {
94+
use core::future::Future;
95+
96+
fn spawn_on(fut: impl Future<Output = ()>) {}
97+
98+
fn demo() {
99+
spawn_on(((|| async move {})()));
100+
}
101+
}

tests/ui/redundant_closure_call_fixable.stderr

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: try not to call a closure in the expression where it is declared
2-
--> $DIR/redundant_closure_call_fixable.rs:16:13
2+
--> $DIR/redundant_closure_call_fixable.rs:17:13
33
|
44
LL | let a = (|| 42)();
55
| ^^^^^^^^^ help: try doing something like: `42`
@@ -8,7 +8,7 @@ LL | let a = (|| 42)();
88
= help: to override `-D warnings` add `#[allow(clippy::redundant_closure_call)]`
99

1010
error: try not to call a closure in the expression where it is declared
11-
--> $DIR/redundant_closure_call_fixable.rs:17:13
11+
--> $DIR/redundant_closure_call_fixable.rs:18:13
1212
|
1313
LL | let b = (async || {
1414
| _____________^
@@ -28,7 +28,7 @@ LL ~ };
2828
|
2929

3030
error: try not to call a closure in the expression where it is declared
31-
--> $DIR/redundant_closure_call_fixable.rs:22:13
31+
--> $DIR/redundant_closure_call_fixable.rs:23:13
3232
|
3333
LL | let c = (|| {
3434
| _____________^
@@ -48,13 +48,13 @@ LL ~ };
4848
|
4949

5050
error: try not to call a closure in the expression where it is declared
51-
--> $DIR/redundant_closure_call_fixable.rs:27:13
51+
--> $DIR/redundant_closure_call_fixable.rs:28:13
5252
|
5353
LL | let d = (async || something().await)();
5454
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try doing something like: `async { something().await }`
5555

5656
error: try not to call a closure in the expression where it is declared
57-
--> $DIR/redundant_closure_call_fixable.rs:36:13
57+
--> $DIR/redundant_closure_call_fixable.rs:37:13
5858
|
5959
LL | (|| m!())()
6060
| ^^^^^^^^^^^ help: try doing something like: `m!()`
@@ -65,7 +65,7 @@ LL | m2!();
6565
= note: this error originates in the macro `m2` (in Nightly builds, run with -Z macro-backtrace for more info)
6666

6767
error: try not to call a closure in the expression where it is declared
68-
--> $DIR/redundant_closure_call_fixable.rs:31:13
68+
--> $DIR/redundant_closure_call_fixable.rs:32:13
6969
|
7070
LL | (|| 0)()
7171
| ^^^^^^^^ help: try doing something like: `0`
@@ -76,52 +76,64 @@ LL | m2!();
7676
= note: this error originates in the macro `m` which comes from the expansion of the macro `m2` (in Nightly builds, run with -Z macro-backtrace for more info)
7777

7878
error: try not to call a closure in the expression where it is declared
79-
--> $DIR/redundant_closure_call_fixable.rs:44:16
79+
--> $DIR/redundant_closure_call_fixable.rs:45:16
8080
|
8181
LL | assert_eq!((|| || 43)()(), 42);
8282
| ^^^^^^^^^^^^^^ help: try doing something like: `43`
8383

8484
error: try not to call a closure in the expression where it is declared
85-
--> $DIR/redundant_closure_call_fixable.rs:53:10
85+
--> $DIR/redundant_closure_call_fixable.rs:54:10
8686
|
8787
LL | dbg!((|| 42)());
8888
| ^^^^^^^^^ help: try doing something like: `42`
8989

9090
error: try not to call a closure in the expression where it is declared
91-
--> $DIR/redundant_closure_call_fixable.rs:56:13
91+
--> $DIR/redundant_closure_call_fixable.rs:57:13
9292
|
9393
LL | let a = (|| || || 123)();
9494
| ^^^^^^^^^^^^^^^^ help: try doing something like: `(|| || 123)`
9595

9696
error: try not to call a closure in the expression where it is declared
97-
--> $DIR/redundant_closure_call_fixable.rs:60:13
97+
--> $DIR/redundant_closure_call_fixable.rs:61:13
9898
|
9999
LL | let a = (|| || || || async || 1)()()()()();
100100
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try doing something like: `async { 1 }`
101101

102102
error: try not to call a closure in the expression where it is declared
103-
--> $DIR/redundant_closure_call_fixable.rs:69:13
103+
--> $DIR/redundant_closure_call_fixable.rs:70:13
104104
|
105105
LL | let a = (|| echo!(|| echo!(|| 1)))()()();
106106
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try doing something like: `1`
107107

108108
error: try not to call a closure in the expression where it is declared
109-
--> $DIR/redundant_closure_call_fixable.rs:71:13
109+
--> $DIR/redundant_closure_call_fixable.rs:72:13
110110
|
111111
LL | let a = (|| echo!((|| 123)))()();
112112
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try doing something like: `123`
113113

114114
error: try not to call a closure in the expression where it is declared
115-
--> $DIR/redundant_closure_call_fixable.rs:84:11
115+
--> $DIR/redundant_closure_call_fixable.rs:85:11
116116
|
117117
LL | bar()((|| || 42)()(), 5);
118118
| ^^^^^^^^^^^^^^ help: try doing something like: `42`
119119

120120
error: try not to call a closure in the expression where it is declared
121-
--> $DIR/redundant_closure_call_fixable.rs:85:9
121+
--> $DIR/redundant_closure_call_fixable.rs:86:9
122122
|
123123
LL | foo((|| || 42)()(), 5);
124124
| ^^^^^^^^^^^^^^ help: try doing something like: `42`
125125

126-
error: aborting due to 14 previous errors
126+
error: try not to call a closure in the expression where it is declared
127+
--> $DIR/redundant_closure_call_fixable.rs:90:5
128+
|
129+
LL | (|| async {})().await;
130+
| ^^^^^^^^^^^^^^^ help: try doing something like: `(async {})`
131+
132+
error: try not to call a closure in the expression where it is declared
133+
--> $DIR/redundant_closure_call_fixable.rs:99:18
134+
|
135+
LL | spawn_on(((|| async move {})()));
136+
| ^^^^^^^^^^^^^^^^^^^^^^ help: try doing something like: `(async move {})`
137+
138+
error: aborting due to 16 previous errors
127139

0 commit comments

Comments
 (0)