about summary refs log tree commit diff
diff options
context:
space:
mode:
authorRyan Mehri <ryan.mehri1@gmail.com>2023-09-21 21:55:10 -0700
committerRyan Mehri <ryan.mehri1@gmail.com>2023-09-21 21:55:10 -0700
commitea118464908589db0293b7ba458a58db2f13db83 (patch)
treef8d5d412e03678a0a7f412de14d89faae1a50273
parent60f7473c997a33cd59d8530f8aa1588bd622d296 (diff)
downloadrust-ea118464908589db0293b7ba458a58db2f13db83.tar.gz
rust-ea118464908589db0293b7ba458a58db2f13db83.zip
fix parens when inlining closure in body of function
-rw-r--r--crates/ide-assists/src/handlers/inline_call.rs31
1 files changed, 30 insertions, 1 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)();
+}
+"#,
+        );
+    }
 }