Skip to content

Commit e71ae68

Browse files
committed
Only inline closure, literal and local arguments when used once
1 parent ac300ea commit e71ae68

File tree

1 file changed

+56
-44
lines changed

1 file changed

+56
-44
lines changed

crates/ide_assists/src/handlers/inline_call.rs

Lines changed: 56 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -157,25 +157,28 @@ pub(crate) fn inline_(
157157

158158
// Inline parameter expressions or generate `let` statements depending on whether inlining works or not.
159159
for ((pat, _), usages, expr) in izip!(params, param_use_nodes, arg_list).rev() {
160+
let expr_is_name_ref = matches!(&expr,
161+
ast::Expr::PathExpr(expr)
162+
if expr.path().and_then(|path| path.as_single_name_ref()).is_some()
163+
);
160164
match &*usages {
161-
// inline single use parameters
162-
[usage] => {
163-
let expr = if matches!(expr, ast::Expr::ClosureExpr(_))
164-
&& usage.syntax().parent().and_then(ast::Expr::cast).is_some()
165-
{
166-
make::expr_paren(expr)
167-
} else {
168-
expr
169-
};
165+
// inline single use closure arguments
166+
[usage]
167+
if matches!(expr, ast::Expr::ClosureExpr(_))
168+
&& usage.syntax().parent().and_then(ast::Expr::cast).is_some() =>
169+
{
170+
cov_mark::hit!(inline_call_inline_closure);
171+
let expr = make::expr_paren(expr);
170172
ted::replace(usage.syntax(), expr.syntax().clone_for_update());
171173
}
172-
// inline parameters whose expression is a simple local reference
173-
[_, ..]
174-
if matches!(&expr,
175-
ast::Expr::PathExpr(expr)
176-
if expr.path().and_then(|path| path.as_single_name_ref()).is_some()
177-
) =>
178-
{
174+
// inline single use literals
175+
[usage] if matches!(expr, ast::Expr::Literal(_)) => {
176+
cov_mark::hit!(inline_call_inline_literal);
177+
ted::replace(usage.syntax(), expr.syntax().clone_for_update());
178+
}
179+
// inline direct local arguments
180+
[_, ..] if expr_is_name_ref => {
181+
cov_mark::hit!(inline_call_inline_locals);
179182
usages.into_iter().for_each(|usage| {
180183
ted::replace(usage.syntax(), &expr.syntax().clone_for_update());
181184
});
@@ -322,7 +325,10 @@ impl Foo {
322325
}
323326
324327
fn main() {
325-
let x = Foo(Foo(3).0 + 2);
328+
let x = {
329+
let this = Foo(3);
330+
Foo(this.0 + 2)
331+
};
326332
}
327333
"#,
328334
);
@@ -355,7 +361,10 @@ impl Foo {
355361
}
356362
357363
fn main() {
358-
let x = Foo(Foo(3).0 + 2);
364+
let x = {
365+
let this = Foo(3);
366+
Foo(this.0 + 2)
367+
};
359368
}
360369
"#,
361370
);
@@ -435,31 +444,6 @@ fn main() {
435444
);
436445
}
437446

438-
#[test]
439-
fn function_single_use_expr_in_param() {
440-
check_assist(
441-
inline_call,
442-
r#"
443-
fn double(x: u32) -> u32 {
444-
2 * x
445-
}
446-
fn main() {
447-
let x = 51;
448-
let x = double$0(10 + x);
449-
}
450-
"#,
451-
r#"
452-
fn double(x: u32) -> u32 {
453-
2 * x
454-
}
455-
fn main() {
456-
let x = 51;
457-
let x = 2 * 10 + x;
458-
}
459-
"#,
460-
);
461-
}
462-
463447
#[test]
464448
fn function_multi_use_expr_in_param() {
465449
check_assist(
@@ -489,7 +473,8 @@ fn main() {
489473
}
490474

491475
#[test]
492-
fn function_multi_use_local_in_param() {
476+
fn function_use_local_in_param() {
477+
cov_mark::check!(inline_call_inline_locals);
493478
check_assist(
494479
inline_call,
495480
r#"
@@ -550,6 +535,7 @@ impl Foo {
550535

551536
#[test]
552537
fn wraps_closure_in_paren() {
538+
cov_mark::check!(inline_call_inline_closure);
553539
check_assist(
554540
inline_call,
555541
r#"
@@ -594,6 +580,32 @@ fn main() {
594580
main();
595581
}
596582
}
583+
"#,
584+
);
585+
}
586+
587+
#[test]
588+
fn inline_single_literal_expr() {
589+
cov_mark::check!(inline_call_inline_literal);
590+
check_assist(
591+
inline_call,
592+
r#"
593+
fn foo(x: u32) -> u32{
594+
x
595+
}
596+
597+
fn main() {
598+
foo$0(222);
599+
}
600+
"#,
601+
r#"
602+
fn foo(x: u32) -> u32{
603+
x
604+
}
605+
606+
fn main() {
607+
222;
608+
}
597609
"#,
598610
);
599611
}

0 commit comments

Comments
 (0)