@@ -17,16 +17,23 @@ use crate::{
17
17
// Inlines a function or method body.
18
18
//
19
19
// ```
20
- // fn align(a: u32, b: u32) -> u32 { (a + b - 1) & !(b - 1) }
20
+ // fn align(a: u32, b: u32) -> u32 {
21
+ // (a + b - 1) & !(b - 1)
22
+ // }
21
23
// fn main() {
22
24
// let x = align$0(1, 2);
23
25
// }
24
26
// ```
25
27
// ->
26
28
// ```
27
- // fn align(a: u32, b: u32) -> u32 { (a + b - 1) & !(b - 1) }
29
+ // fn align(a: u32, b: u32) -> u32 {
30
+ // (a + b - 1) & !(b - 1)
31
+ // }
28
32
// fn main() {
29
- // let x = { let b = 2; (1 + b - 1) & !(b - 1) };
33
+ // let x = {
34
+ // let b = 2;
35
+ // (1 + b - 1) & !(b - 1)
36
+ // };
30
37
// }
31
38
// ```
32
39
pub ( crate ) fn inline_call ( acc : & mut Assists , ctx : & AssistContext ) -> Option < ( ) > {
@@ -140,14 +147,14 @@ pub(crate) fn inline_(
140
147
141
148
// Rewrite `self` to `this`
142
149
if param_list. self_param ( ) . is_some ( ) {
143
- let this = make:: name_ref ( "this" ) . syntax ( ) . clone_for_update ( ) ;
150
+ let this = || make:: name_ref ( "this" ) . syntax ( ) . clone_for_update ( ) ;
144
151
usages_for_locals ( params[ 0 ] . 1 . as_local ( ctx. sema . db ) )
145
152
. flat_map ( |FileReference { name, range, .. } | match name {
146
153
ast:: NameLike :: NameRef ( _) => Some ( body. syntax ( ) . covering_element ( range) ) ,
147
154
_ => None ,
148
155
} )
149
156
. for_each ( |it| {
150
- ted:: replace ( it, & this) ;
157
+ ted:: replace ( it, & this ( ) ) ;
151
158
} )
152
159
}
153
160
@@ -212,37 +219,44 @@ fn main() {
212
219
) ;
213
220
}
214
221
222
+ #[ test]
223
+ fn not_applicable_when_incorrect_number_of_parameters_are_provided ( ) {
224
+ cov_mark:: check!( inline_call_incorrect_number_of_arguments) ;
225
+ check_assist_not_applicable (
226
+ inline_call,
227
+ r#"
228
+ fn add(a: u32, b: u32) -> u32 { a + b }
229
+ fn main() { let x = add$0(42); }
230
+ "# ,
231
+ ) ;
232
+ }
233
+
215
234
#[ test]
216
235
fn args_with_side_effects ( ) {
217
236
check_assist (
218
237
inline_call,
219
238
r#"
220
- fn foo(name: String) { println!("Hello, {}!", name); }
239
+ fn foo(name: String) {
240
+ println!("Hello, {}!", name);
241
+ }
221
242
fn main() {
222
243
foo$0(String::from("Michael"));
223
244
}
224
245
"# ,
225
246
r#"
226
- fn foo(name: String) { println!("Hello, {}!", name); }
247
+ fn foo(name: String) {
248
+ println!("Hello, {}!", name);
249
+ }
227
250
fn main() {
228
- { let name = String::from("Michael"); println!("Hello, {}!", name); };
251
+ {
252
+ let name = String::from("Michael");
253
+ println!("Hello, {}!", name);
254
+ };
229
255
}
230
256
"# ,
231
257
) ;
232
258
}
233
259
234
- #[ test]
235
- fn not_applicable_when_incorrect_number_of_parameters_are_provided ( ) {
236
- cov_mark:: check!( inline_call_incorrect_number_of_arguments) ;
237
- check_assist_not_applicable (
238
- inline_call,
239
- r#"
240
- fn add(a: u32, b: u32) -> u32 { a + b }
241
- fn main() { let x = add$0(42); }
242
- "# ,
243
- ) ;
244
- }
245
-
246
260
#[ test]
247
261
fn function_with_multiple_statements ( ) {
248
262
check_assist (
@@ -266,7 +280,8 @@ fn foo(a: u32, b: u32) -> u32 {
266
280
}
267
281
268
282
fn main() {
269
- let x = { let b = 2;
283
+ let x = {
284
+ let b = 2;
270
285
let x = 1 + b;
271
286
let y = x - b;
272
287
x * y
@@ -369,7 +384,8 @@ impl Foo {
369
384
}
370
385
371
386
fn main() {
372
- let x = { let ref this = Foo(3);
387
+ let x = {
388
+ let ref this = Foo(3);
373
389
Foo(this.0 + 2)
374
390
};
375
391
}
@@ -406,7 +422,8 @@ impl Foo {
406
422
407
423
fn main() {
408
424
let mut foo = Foo(3);
409
- { let ref mut this = foo;
425
+ {
426
+ let ref mut this = foo;
410
427
this.0 = 0;
411
428
};
412
429
}
@@ -458,7 +475,8 @@ fn square(x: u32) -> u32 {
458
475
}
459
476
fn main() {
460
477
let x = 51;
461
- let y = { let x = 10 + x;
478
+ let y = {
479
+ let x = 10 + x;
462
480
x * x
463
481
};
464
482
}
@@ -487,6 +505,41 @@ fn main() {
487
505
let local = 51;
488
506
let y = local * local;
489
507
}
508
+ "# ,
509
+ ) ;
510
+ }
511
+
512
+ #[ test]
513
+ fn method_in_impl ( ) {
514
+ check_assist (
515
+ inline_call,
516
+ r#"
517
+ struct Foo;
518
+ impl Foo {
519
+ fn foo(&self) {
520
+ self;
521
+ self;
522
+ }
523
+ fn bar(&self) {
524
+ self.foo$0();
525
+ }
526
+ }
527
+ "# ,
528
+ r#"
529
+ struct Foo;
530
+ impl Foo {
531
+ fn foo(&self) {
532
+ self;
533
+ self;
534
+ }
535
+ fn bar(&self) {
536
+ {
537
+ let ref this = self;
538
+ this;
539
+ this;
540
+ };
541
+ }
542
+ }
490
543
"# ,
491
544
) ;
492
545
}
0 commit comments