Skip to content

Commit 533cb21

Browse files
committed
Fix redundant_closure_call for single-expression async closures
1 parent 7fbcbff commit 533cb21

File tree

4 files changed

+15
-2
lines changed

4 files changed

+15
-2
lines changed

clippy_lints/src/redundant_closure_call.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,12 @@ impl EarlyLintPass for RedundantClosureCall {
8585
let mut hint =
8686
snippet_with_applicability(cx, block.span, "..", &mut app).into_owned();
8787
if r#async.is_async() {
88-
hint = format!("async {}", hint);
88+
if let ast::ExprKind::Block(_, _) = block.kind {
89+
hint = format!("async {}", hint);
90+
} else {
91+
// `async x` is a syntax error, so it becomes `async { x }`
92+
hint = format!("async {{ {} }}", hint);
93+
}
8994
}
9095
diag.span_suggestion(expr.span, "try doing something like", hint, app);
9196
}

tests/ui/redundant_closure_call_fixable.fixed

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ fn main() {
2424
let y = 2;
2525
x * y
2626
};
27+
let d = async { something().await };
2728
}

tests/ui/redundant_closure_call_fixable.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ fn main() {
2424
let y = 2;
2525
x * y
2626
})();
27+
let d = (async || something().await)();
2728
}

tests/ui/redundant_closure_call_fixable.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,11 @@ LL + x * y
4646
LL ~ };
4747
|
4848

49-
error: aborting due to 3 previous errors
49+
error: try not to call a closure in the expression where it is declared
50+
--> $DIR/redundant_closure_call_fixable.rs:27:13
51+
|
52+
LL | let d = (async || something().await)();
53+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try doing something like: `async { something().await }`
54+
55+
error: aborting due to 4 previous errors
5056

0 commit comments

Comments
 (0)