@@ -157,25 +157,28 @@ pub(crate) fn inline_(
157
157
158
158
// Inline parameter expressions or generate `let` statements depending on whether inlining works or not.
159
159
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
+ ) ;
160
164
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) ;
170
172
ted:: replace ( usage. syntax ( ) , expr. syntax ( ) . clone_for_update ( ) ) ;
171
173
}
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) ;
179
182
usages. into_iter ( ) . for_each ( |usage| {
180
183
ted:: replace ( usage. syntax ( ) , & expr. syntax ( ) . clone_for_update ( ) ) ;
181
184
} ) ;
@@ -322,7 +325,10 @@ impl Foo {
322
325
}
323
326
324
327
fn main() {
325
- let x = Foo(Foo(3).0 + 2);
328
+ let x = {
329
+ let this = Foo(3);
330
+ Foo(this.0 + 2)
331
+ };
326
332
}
327
333
"# ,
328
334
) ;
@@ -355,7 +361,10 @@ impl Foo {
355
361
}
356
362
357
363
fn main() {
358
- let x = Foo(Foo(3).0 + 2);
364
+ let x = {
365
+ let this = Foo(3);
366
+ Foo(this.0 + 2)
367
+ };
359
368
}
360
369
"# ,
361
370
) ;
@@ -435,31 +444,6 @@ fn main() {
435
444
) ;
436
445
}
437
446
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
-
463
447
#[ test]
464
448
fn function_multi_use_expr_in_param ( ) {
465
449
check_assist (
@@ -489,7 +473,8 @@ fn main() {
489
473
}
490
474
491
475
#[ test]
492
- fn function_multi_use_local_in_param ( ) {
476
+ fn function_use_local_in_param ( ) {
477
+ cov_mark:: check!( inline_call_inline_locals) ;
493
478
check_assist (
494
479
inline_call,
495
480
r#"
@@ -550,6 +535,7 @@ impl Foo {
550
535
551
536
#[ test]
552
537
fn wraps_closure_in_paren ( ) {
538
+ cov_mark:: check!( inline_call_inline_closure) ;
553
539
check_assist (
554
540
inline_call,
555
541
r#"
@@ -594,6 +580,32 @@ fn main() {
594
580
main();
595
581
}
596
582
}
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
+ }
597
609
"# ,
598
610
) ;
599
611
}
0 commit comments