diff options
| author | bors <bors@rust-lang.org> | 2023-09-22 05:07:51 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2023-09-22 05:07:51 +0000 |
| commit | c22bb0338a20a67b40e435e51de70867fed1ab95 (patch) | |
| tree | f8d5d412e03678a0a7f412de14d89faae1a50273 | |
| parent | d6fef2c7e3be2783369b4b84e320b163b8fd291a (diff) | |
| parent | ea118464908589db0293b7ba458a58db2f13db83 (diff) | |
| download | rust-c22bb0338a20a67b40e435e51de70867fed1ab95.tar.gz rust-c22bb0338a20a67b40e435e51de70867fed1ab95.zip | |
Auto merge of #15651 - rmehri01:15639_fix_inline_local_closure, r=lnicola
Fix inlining closures from local variables and functions
Previously, closures were not properly wrapped in parentheses for the `inline_local_variable` and `inline_call` assists, leading to the usages being incorrectly called:
```rust
fn main() {
let $0f = || 2;
let _ = f();
}
```
Now produces:
```rust
fn main() {
let _ = (|| 2)();
}
```
Instead of:
```rust
fn main() {
let _ = || 2();
}
```
Closes #15639
| -rw-r--r-- | crates/ide-assists/src/handlers/inline_call.rs | 31 | ||||
| -rw-r--r-- | crates/ide-assists/src/handlers/inline_local_variable.rs | 21 |
2 files changed, 49 insertions, 3 deletions
diff --git a/crates/ide-assists/src/handlers/inline_call.rs b/crates/ide-assists/src/handlers/inline_call.rs index ffab58509b1..a80c1e23941 100644 --- a/crates/ide-assists/src/handlers/inline_call.rs +++ b/crates/ide-assists/src/handlers/inline_call.rs @@ -481,8 +481,12 @@ fn inline( }; body.reindent_to(original_indentation); + let no_stmts = body.statements().next().is_none(); match body.tail_expr() { - Some(expr) if !is_async_fn && body.statements().next().is_none() => expr, + Some(expr) if matches!(expr, ast::Expr::ClosureExpr(_)) && no_stmts => { + make::expr_paren(expr).clone_for_update() + } + Some(expr) if !is_async_fn && no_stmts => expr, _ => match node .syntax() .parent() @@ -1474,4 +1478,29 @@ fn main() { "#, ); } + + #[test] + fn inline_call_closure_body() { + check_assist( + inline_call, + r#" +fn f() -> impl Fn() -> i32 { + || 2 +} + +fn main() { + let _ = $0f()(); +} +"#, + r#" +fn f() -> impl Fn() -> i32 { + || 2 +} + +fn main() { + let _ = (|| 2)(); +} +"#, + ); + } } diff --git a/crates/ide-assists/src/handlers/inline_local_variable.rs b/crates/ide-assists/src/handlers/inline_local_variable.rs index e69d1a29677..49dcde75d2b 100644 --- a/crates/ide-assists/src/handlers/inline_local_variable.rs +++ b/crates/ide-assists/src/handlers/inline_local_variable.rs @@ -96,8 +96,7 @@ pub(crate) fn inline_local_variable(acc: &mut Assists, ctx: &AssistContext<'_>) ); let parent = matches!( usage_parent, - ast::Expr::CallExpr(_) - | ast::Expr::TupleExpr(_) + ast::Expr::TupleExpr(_) | ast::Expr::ArrayExpr(_) | ast::Expr::ParenExpr(_) | ast::Expr::ForExpr(_) @@ -952,4 +951,22 @@ fn f() { "#, ); } + + #[test] + fn test_inline_closure() { + check_assist( + inline_local_variable, + r#" +fn main() { + let $0f = || 2; + let _ = f(); +} +"#, + r#" +fn main() { + let _ = (|| 2)(); +} +"#, + ); + } } |
