diff options
Diffstat (limited to 'crates/ra_assists/src/handlers')
-rw-r--r-- | crates/ra_assists/src/handlers/inline_local_variable.rs | 122 |
1 files changed, 72 insertions, 50 deletions
diff --git a/crates/ra_assists/src/handlers/inline_local_variable.rs b/crates/ra_assists/src/handlers/inline_local_variable.rs index 53a72309b..eb5112343 100644 --- a/crates/ra_assists/src/handlers/inline_local_variable.rs +++ b/crates/ra_assists/src/handlers/inline_local_variable.rs | |||
@@ -2,9 +2,9 @@ use ra_syntax::{ | |||
2 | ast::{self, AstNode, AstToken}, | 2 | ast::{self, AstNode, AstToken}, |
3 | TextRange, | 3 | TextRange, |
4 | }; | 4 | }; |
5 | use test_utils::tested_by; | ||
5 | 6 | ||
6 | use crate::assist_ctx::ActionBuilder; | 7 | use crate::{assist_ctx::ActionBuilder, Assist, AssistCtx, AssistId}; |
7 | use crate::{Assist, AssistCtx, AssistId}; | ||
8 | 8 | ||
9 | // Assist: inline_local_variable | 9 | // Assist: inline_local_variable |
10 | // | 10 | // |
@@ -29,6 +29,11 @@ pub(crate) fn inline_local_variable(ctx: AssistCtx) -> Option<Assist> { | |||
29 | _ => return None, | 29 | _ => return None, |
30 | }; | 30 | }; |
31 | if bind_pat.is_mutable() { | 31 | if bind_pat.is_mutable() { |
32 | tested_by!(test_not_inline_mut_variable); | ||
33 | return None; | ||
34 | } | ||
35 | if !bind_pat.syntax().text_range().contains_inclusive(ctx.frange.range.start()) { | ||
36 | tested_by!(not_applicable_outside_of_bind_pat); | ||
32 | return None; | 37 | return None; |
33 | } | 38 | } |
34 | let initializer_expr = let_stmt.initializer()?; | 39 | let initializer_expr = let_stmt.initializer()?; |
@@ -111,6 +116,8 @@ pub(crate) fn inline_local_variable(ctx: AssistCtx) -> Option<Assist> { | |||
111 | 116 | ||
112 | #[cfg(test)] | 117 | #[cfg(test)] |
113 | mod tests { | 118 | mod tests { |
119 | use test_utils::covers; | ||
120 | |||
114 | use crate::helpers::{check_assist, check_assist_not_applicable}; | 121 | use crate::helpers::{check_assist, check_assist_not_applicable}; |
115 | 122 | ||
116 | use super::*; | 123 | use super::*; |
@@ -119,7 +126,7 @@ mod tests { | |||
119 | fn test_inline_let_bind_literal_expr() { | 126 | fn test_inline_let_bind_literal_expr() { |
120 | check_assist( | 127 | check_assist( |
121 | inline_local_variable, | 128 | inline_local_variable, |
122 | " | 129 | r" |
123 | fn bar(a: usize) {} | 130 | fn bar(a: usize) {} |
124 | fn foo() { | 131 | fn foo() { |
125 | let a<|> = 1; | 132 | let a<|> = 1; |
@@ -133,7 +140,7 @@ fn foo() { | |||
133 | let b = a * 10; | 140 | let b = a * 10; |
134 | bar(a); | 141 | bar(a); |
135 | }", | 142 | }", |
136 | " | 143 | r" |
137 | fn bar(a: usize) {} | 144 | fn bar(a: usize) {} |
138 | fn foo() { | 145 | fn foo() { |
139 | <|>1 + 1; | 146 | <|>1 + 1; |
@@ -153,7 +160,7 @@ fn foo() { | |||
153 | fn test_inline_let_bind_bin_expr() { | 160 | fn test_inline_let_bind_bin_expr() { |
154 | check_assist( | 161 | check_assist( |
155 | inline_local_variable, | 162 | inline_local_variable, |
156 | " | 163 | r" |
157 | fn bar(a: usize) {} | 164 | fn bar(a: usize) {} |
158 | fn foo() { | 165 | fn foo() { |
159 | let a<|> = 1 + 1; | 166 | let a<|> = 1 + 1; |
@@ -167,7 +174,7 @@ fn foo() { | |||
167 | let b = a * 10; | 174 | let b = a * 10; |
168 | bar(a); | 175 | bar(a); |
169 | }", | 176 | }", |
170 | " | 177 | r" |
171 | fn bar(a: usize) {} | 178 | fn bar(a: usize) {} |
172 | fn foo() { | 179 | fn foo() { |
173 | <|>(1 + 1) + 1; | 180 | <|>(1 + 1) + 1; |
@@ -187,7 +194,7 @@ fn foo() { | |||
187 | fn test_inline_let_bind_function_call_expr() { | 194 | fn test_inline_let_bind_function_call_expr() { |
188 | check_assist( | 195 | check_assist( |
189 | inline_local_variable, | 196 | inline_local_variable, |
190 | " | 197 | r" |
191 | fn bar(a: usize) {} | 198 | fn bar(a: usize) {} |
192 | fn foo() { | 199 | fn foo() { |
193 | let a<|> = bar(1); | 200 | let a<|> = bar(1); |
@@ -201,7 +208,7 @@ fn foo() { | |||
201 | let b = a * 10; | 208 | let b = a * 10; |
202 | bar(a); | 209 | bar(a); |
203 | }", | 210 | }", |
204 | " | 211 | r" |
205 | fn bar(a: usize) {} | 212 | fn bar(a: usize) {} |
206 | fn foo() { | 213 | fn foo() { |
207 | <|>bar(1) + 1; | 214 | <|>bar(1) + 1; |
@@ -221,7 +228,7 @@ fn foo() { | |||
221 | fn test_inline_let_bind_cast_expr() { | 228 | fn test_inline_let_bind_cast_expr() { |
222 | check_assist( | 229 | check_assist( |
223 | inline_local_variable, | 230 | inline_local_variable, |
224 | " | 231 | r" |
225 | fn bar(a: usize): usize { a } | 232 | fn bar(a: usize): usize { a } |
226 | fn foo() { | 233 | fn foo() { |
227 | let a<|> = bar(1) as u64; | 234 | let a<|> = bar(1) as u64; |
@@ -235,7 +242,7 @@ fn foo() { | |||
235 | let b = a * 10; | 242 | let b = a * 10; |
236 | bar(a); | 243 | bar(a); |
237 | }", | 244 | }", |
238 | " | 245 | r" |
239 | fn bar(a: usize): usize { a } | 246 | fn bar(a: usize): usize { a } |
240 | fn foo() { | 247 | fn foo() { |
241 | <|>(bar(1) as u64) + 1; | 248 | <|>(bar(1) as u64) + 1; |
@@ -255,7 +262,7 @@ fn foo() { | |||
255 | fn test_inline_let_bind_block_expr() { | 262 | fn test_inline_let_bind_block_expr() { |
256 | check_assist( | 263 | check_assist( |
257 | inline_local_variable, | 264 | inline_local_variable, |
258 | " | 265 | r" |
259 | fn foo() { | 266 | fn foo() { |
260 | let a<|> = { 10 + 1 }; | 267 | let a<|> = { 10 + 1 }; |
261 | a + 1; | 268 | a + 1; |
@@ -268,7 +275,7 @@ fn foo() { | |||
268 | let b = a * 10; | 275 | let b = a * 10; |
269 | bar(a); | 276 | bar(a); |
270 | }", | 277 | }", |
271 | " | 278 | r" |
272 | fn foo() { | 279 | fn foo() { |
273 | <|>{ 10 + 1 } + 1; | 280 | <|>{ 10 + 1 } + 1; |
274 | if { 10 + 1 } > 10 { | 281 | if { 10 + 1 } > 10 { |
@@ -287,7 +294,7 @@ fn foo() { | |||
287 | fn test_inline_let_bind_paren_expr() { | 294 | fn test_inline_let_bind_paren_expr() { |
288 | check_assist( | 295 | check_assist( |
289 | inline_local_variable, | 296 | inline_local_variable, |
290 | " | 297 | r" |
291 | fn foo() { | 298 | fn foo() { |
292 | let a<|> = ( 10 + 1 ); | 299 | let a<|> = ( 10 + 1 ); |
293 | a + 1; | 300 | a + 1; |
@@ -300,7 +307,7 @@ fn foo() { | |||
300 | let b = a * 10; | 307 | let b = a * 10; |
301 | bar(a); | 308 | bar(a); |
302 | }", | 309 | }", |
303 | " | 310 | r" |
304 | fn foo() { | 311 | fn foo() { |
305 | <|>( 10 + 1 ) + 1; | 312 | <|>( 10 + 1 ) + 1; |
306 | if ( 10 + 1 ) > 10 { | 313 | if ( 10 + 1 ) > 10 { |
@@ -317,9 +324,10 @@ fn foo() { | |||
317 | 324 | ||
318 | #[test] | 325 | #[test] |
319 | fn test_not_inline_mut_variable() { | 326 | fn test_not_inline_mut_variable() { |
327 | covers!(test_not_inline_mut_variable); | ||
320 | check_assist_not_applicable( | 328 | check_assist_not_applicable( |
321 | inline_local_variable, | 329 | inline_local_variable, |
322 | " | 330 | r" |
323 | fn foo() { | 331 | fn foo() { |
324 | let mut a<|> = 1 + 1; | 332 | let mut a<|> = 1 + 1; |
325 | a + 1; | 333 | a + 1; |
@@ -331,13 +339,13 @@ fn foo() { | |||
331 | fn test_call_expr() { | 339 | fn test_call_expr() { |
332 | check_assist( | 340 | check_assist( |
333 | inline_local_variable, | 341 | inline_local_variable, |
334 | " | 342 | r" |
335 | fn foo() { | 343 | fn foo() { |
336 | let a<|> = bar(10 + 1); | 344 | let a<|> = bar(10 + 1); |
337 | let b = a * 10; | 345 | let b = a * 10; |
338 | let c = a as usize; | 346 | let c = a as usize; |
339 | }", | 347 | }", |
340 | " | 348 | r" |
341 | fn foo() { | 349 | fn foo() { |
342 | <|>let b = bar(10 + 1) * 10; | 350 | <|>let b = bar(10 + 1) * 10; |
343 | let c = bar(10 + 1) as usize; | 351 | let c = bar(10 + 1) as usize; |
@@ -349,14 +357,14 @@ fn foo() { | |||
349 | fn test_index_expr() { | 357 | fn test_index_expr() { |
350 | check_assist( | 358 | check_assist( |
351 | inline_local_variable, | 359 | inline_local_variable, |
352 | " | 360 | r" |
353 | fn foo() { | 361 | fn foo() { |
354 | let x = vec![1, 2, 3]; | 362 | let x = vec![1, 2, 3]; |
355 | let a<|> = x[0]; | 363 | let a<|> = x[0]; |
356 | let b = a * 10; | 364 | let b = a * 10; |
357 | let c = a as usize; | 365 | let c = a as usize; |
358 | }", | 366 | }", |
359 | " | 367 | r" |
360 | fn foo() { | 368 | fn foo() { |
361 | let x = vec![1, 2, 3]; | 369 | let x = vec![1, 2, 3]; |
362 | <|>let b = x[0] * 10; | 370 | <|>let b = x[0] * 10; |
@@ -369,14 +377,14 @@ fn foo() { | |||
369 | fn test_method_call_expr() { | 377 | fn test_method_call_expr() { |
370 | check_assist( | 378 | check_assist( |
371 | inline_local_variable, | 379 | inline_local_variable, |
372 | " | 380 | r" |
373 | fn foo() { | 381 | fn foo() { |
374 | let bar = vec![1]; | 382 | let bar = vec![1]; |
375 | let a<|> = bar.len(); | 383 | let a<|> = bar.len(); |
376 | let b = a * 10; | 384 | let b = a * 10; |
377 | let c = a as usize; | 385 | let c = a as usize; |
378 | }", | 386 | }", |
379 | " | 387 | r" |
380 | fn foo() { | 388 | fn foo() { |
381 | let bar = vec![1]; | 389 | let bar = vec![1]; |
382 | <|>let b = bar.len() * 10; | 390 | <|>let b = bar.len() * 10; |
@@ -389,7 +397,7 @@ fn foo() { | |||
389 | fn test_field_expr() { | 397 | fn test_field_expr() { |
390 | check_assist( | 398 | check_assist( |
391 | inline_local_variable, | 399 | inline_local_variable, |
392 | " | 400 | r" |
393 | struct Bar { | 401 | struct Bar { |
394 | foo: usize | 402 | foo: usize |
395 | } | 403 | } |
@@ -400,7 +408,7 @@ fn foo() { | |||
400 | let b = a * 10; | 408 | let b = a * 10; |
401 | let c = a as usize; | 409 | let c = a as usize; |
402 | }", | 410 | }", |
403 | " | 411 | r" |
404 | struct Bar { | 412 | struct Bar { |
405 | foo: usize | 413 | foo: usize |
406 | } | 414 | } |
@@ -417,7 +425,7 @@ fn foo() { | |||
417 | fn test_try_expr() { | 425 | fn test_try_expr() { |
418 | check_assist( | 426 | check_assist( |
419 | inline_local_variable, | 427 | inline_local_variable, |
420 | " | 428 | r" |
421 | fn foo() -> Option<usize> { | 429 | fn foo() -> Option<usize> { |
422 | let bar = Some(1); | 430 | let bar = Some(1); |
423 | let a<|> = bar?; | 431 | let a<|> = bar?; |
@@ -425,7 +433,7 @@ fn foo() -> Option<usize> { | |||
425 | let c = a as usize; | 433 | let c = a as usize; |
426 | None | 434 | None |
427 | }", | 435 | }", |
428 | " | 436 | r" |
429 | fn foo() -> Option<usize> { | 437 | fn foo() -> Option<usize> { |
430 | let bar = Some(1); | 438 | let bar = Some(1); |
431 | <|>let b = bar? * 10; | 439 | <|>let b = bar? * 10; |
@@ -439,13 +447,13 @@ fn foo() -> Option<usize> { | |||
439 | fn test_ref_expr() { | 447 | fn test_ref_expr() { |
440 | check_assist( | 448 | check_assist( |
441 | inline_local_variable, | 449 | inline_local_variable, |
442 | " | 450 | r" |
443 | fn foo() { | 451 | fn foo() { |
444 | let bar = 10; | 452 | let bar = 10; |
445 | let a<|> = &bar; | 453 | let a<|> = &bar; |
446 | let b = a * 10; | 454 | let b = a * 10; |
447 | }", | 455 | }", |
448 | " | 456 | r" |
449 | fn foo() { | 457 | fn foo() { |
450 | let bar = 10; | 458 | let bar = 10; |
451 | <|>let b = &bar * 10; | 459 | <|>let b = &bar * 10; |
@@ -457,12 +465,12 @@ fn foo() { | |||
457 | fn test_tuple_expr() { | 465 | fn test_tuple_expr() { |
458 | check_assist( | 466 | check_assist( |
459 | inline_local_variable, | 467 | inline_local_variable, |
460 | " | 468 | r" |
461 | fn foo() { | 469 | fn foo() { |
462 | let a<|> = (10, 20); | 470 | let a<|> = (10, 20); |
463 | let b = a[0]; | 471 | let b = a[0]; |
464 | }", | 472 | }", |
465 | " | 473 | r" |
466 | fn foo() { | 474 | fn foo() { |
467 | <|>let b = (10, 20)[0]; | 475 | <|>let b = (10, 20)[0]; |
468 | }", | 476 | }", |
@@ -473,12 +481,12 @@ fn foo() { | |||
473 | fn test_array_expr() { | 481 | fn test_array_expr() { |
474 | check_assist( | 482 | check_assist( |
475 | inline_local_variable, | 483 | inline_local_variable, |
476 | " | 484 | r" |
477 | fn foo() { | 485 | fn foo() { |
478 | let a<|> = [1, 2, 3]; | 486 | let a<|> = [1, 2, 3]; |
479 | let b = a.len(); | 487 | let b = a.len(); |
480 | }", | 488 | }", |
481 | " | 489 | r" |
482 | fn foo() { | 490 | fn foo() { |
483 | <|>let b = [1, 2, 3].len(); | 491 | <|>let b = [1, 2, 3].len(); |
484 | }", | 492 | }", |
@@ -489,13 +497,13 @@ fn foo() { | |||
489 | fn test_paren() { | 497 | fn test_paren() { |
490 | check_assist( | 498 | check_assist( |
491 | inline_local_variable, | 499 | inline_local_variable, |
492 | " | 500 | r" |
493 | fn foo() { | 501 | fn foo() { |
494 | let a<|> = (10 + 20); | 502 | let a<|> = (10 + 20); |
495 | let b = a * 10; | 503 | let b = a * 10; |
496 | let c = a as usize; | 504 | let c = a as usize; |
497 | }", | 505 | }", |
498 | " | 506 | r" |
499 | fn foo() { | 507 | fn foo() { |
500 | <|>let b = (10 + 20) * 10; | 508 | <|>let b = (10 + 20) * 10; |
501 | let c = (10 + 20) as usize; | 509 | let c = (10 + 20) as usize; |
@@ -507,14 +515,14 @@ fn foo() { | |||
507 | fn test_path_expr() { | 515 | fn test_path_expr() { |
508 | check_assist( | 516 | check_assist( |
509 | inline_local_variable, | 517 | inline_local_variable, |
510 | " | 518 | r" |
511 | fn foo() { | 519 | fn foo() { |
512 | let d = 10; | 520 | let d = 10; |
513 | let a<|> = d; | 521 | let a<|> = d; |
514 | let b = a * 10; | 522 | let b = a * 10; |
515 | let c = a as usize; | 523 | let c = a as usize; |
516 | }", | 524 | }", |
517 | " | 525 | r" |
518 | fn foo() { | 526 | fn foo() { |
519 | let d = 10; | 527 | let d = 10; |
520 | <|>let b = d * 10; | 528 | <|>let b = d * 10; |
@@ -527,13 +535,13 @@ fn foo() { | |||
527 | fn test_block_expr() { | 535 | fn test_block_expr() { |
528 | check_assist( | 536 | check_assist( |
529 | inline_local_variable, | 537 | inline_local_variable, |
530 | " | 538 | r" |
531 | fn foo() { | 539 | fn foo() { |
532 | let a<|> = { 10 }; | 540 | let a<|> = { 10 }; |
533 | let b = a * 10; | 541 | let b = a * 10; |
534 | let c = a as usize; | 542 | let c = a as usize; |
535 | }", | 543 | }", |
536 | " | 544 | r" |
537 | fn foo() { | 545 | fn foo() { |
538 | <|>let b = { 10 } * 10; | 546 | <|>let b = { 10 } * 10; |
539 | let c = { 10 } as usize; | 547 | let c = { 10 } as usize; |
@@ -545,7 +553,7 @@ fn foo() { | |||
545 | fn test_used_in_different_expr1() { | 553 | fn test_used_in_different_expr1() { |
546 | check_assist( | 554 | check_assist( |
547 | inline_local_variable, | 555 | inline_local_variable, |
548 | " | 556 | r" |
549 | fn foo() { | 557 | fn foo() { |
550 | let a<|> = 10 + 20; | 558 | let a<|> = 10 + 20; |
551 | let b = a * 10; | 559 | let b = a * 10; |
@@ -553,7 +561,7 @@ fn foo() { | |||
553 | let d = [a, 10]; | 561 | let d = [a, 10]; |
554 | let e = (a); | 562 | let e = (a); |
555 | }", | 563 | }", |
556 | " | 564 | r" |
557 | fn foo() { | 565 | fn foo() { |
558 | <|>let b = (10 + 20) * 10; | 566 | <|>let b = (10 + 20) * 10; |
559 | let c = (10 + 20, 20); | 567 | let c = (10 + 20, 20); |
@@ -567,12 +575,12 @@ fn foo() { | |||
567 | fn test_used_in_for_expr() { | 575 | fn test_used_in_for_expr() { |
568 | check_assist( | 576 | check_assist( |
569 | inline_local_variable, | 577 | inline_local_variable, |
570 | " | 578 | r" |
571 | fn foo() { | 579 | fn foo() { |
572 | let a<|> = vec![10, 20]; | 580 | let a<|> = vec![10, 20]; |
573 | for i in a {} | 581 | for i in a {} |
574 | }", | 582 | }", |
575 | " | 583 | r" |
576 | fn foo() { | 584 | fn foo() { |
577 | <|>for i in vec![10, 20] {} | 585 | <|>for i in vec![10, 20] {} |
578 | }", | 586 | }", |
@@ -583,12 +591,12 @@ fn foo() { | |||
583 | fn test_used_in_while_expr() { | 591 | fn test_used_in_while_expr() { |
584 | check_assist( | 592 | check_assist( |
585 | inline_local_variable, | 593 | inline_local_variable, |
586 | " | 594 | r" |
587 | fn foo() { | 595 | fn foo() { |
588 | let a<|> = 1 > 0; | 596 | let a<|> = 1 > 0; |
589 | while a {} | 597 | while a {} |
590 | }", | 598 | }", |
591 | " | 599 | r" |
592 | fn foo() { | 600 | fn foo() { |
593 | <|>while 1 > 0 {} | 601 | <|>while 1 > 0 {} |
594 | }", | 602 | }", |
@@ -599,14 +607,14 @@ fn foo() { | |||
599 | fn test_used_in_break_expr() { | 607 | fn test_used_in_break_expr() { |
600 | check_assist( | 608 | check_assist( |
601 | inline_local_variable, | 609 | inline_local_variable, |
602 | " | 610 | r" |
603 | fn foo() { | 611 | fn foo() { |
604 | let a<|> = 1 + 1; | 612 | let a<|> = 1 + 1; |
605 | loop { | 613 | loop { |
606 | break a; | 614 | break a; |
607 | } | 615 | } |
608 | }", | 616 | }", |
609 | " | 617 | r" |
610 | fn foo() { | 618 | fn foo() { |
611 | <|>loop { | 619 | <|>loop { |
612 | break 1 + 1; | 620 | break 1 + 1; |
@@ -619,12 +627,12 @@ fn foo() { | |||
619 | fn test_used_in_return_expr() { | 627 | fn test_used_in_return_expr() { |
620 | check_assist( | 628 | check_assist( |
621 | inline_local_variable, | 629 | inline_local_variable, |
622 | " | 630 | r" |
623 | fn foo() { | 631 | fn foo() { |
624 | let a<|> = 1 > 0; | 632 | let a<|> = 1 > 0; |
625 | return a; | 633 | return a; |
626 | }", | 634 | }", |
627 | " | 635 | r" |
628 | fn foo() { | 636 | fn foo() { |
629 | <|>return 1 > 0; | 637 | <|>return 1 > 0; |
630 | }", | 638 | }", |
@@ -635,12 +643,12 @@ fn foo() { | |||
635 | fn test_used_in_match_expr() { | 643 | fn test_used_in_match_expr() { |
636 | check_assist( | 644 | check_assist( |
637 | inline_local_variable, | 645 | inline_local_variable, |
638 | " | 646 | r" |
639 | fn foo() { | 647 | fn foo() { |
640 | let a<|> = 1 > 0; | 648 | let a<|> = 1 > 0; |
641 | match a {} | 649 | match a {} |
642 | }", | 650 | }", |
643 | " | 651 | r" |
644 | fn foo() { | 652 | fn foo() { |
645 | <|>match 1 > 0 {} | 653 | <|>match 1 > 0 {} |
646 | }", | 654 | }", |
@@ -651,11 +659,25 @@ fn foo() { | |||
651 | fn test_not_applicable_if_variable_unused() { | 659 | fn test_not_applicable_if_variable_unused() { |
652 | check_assist_not_applicable( | 660 | check_assist_not_applicable( |
653 | inline_local_variable, | 661 | inline_local_variable, |
654 | " | 662 | r" |
655 | fn foo() { | 663 | fn foo() { |
656 | let <|>a = 0; | 664 | let <|>a = 0; |
657 | } | 665 | } |
658 | ", | 666 | ", |
659 | ) | 667 | ) |
660 | } | 668 | } |
669 | |||
670 | #[test] | ||
671 | fn not_applicable_outside_of_bind_pat() { | ||
672 | covers!(not_applicable_outside_of_bind_pat); | ||
673 | check_assist_not_applicable( | ||
674 | inline_local_variable, | ||
675 | r" | ||
676 | fn main() { | ||
677 | let x = <|>1 + 2; | ||
678 | x * 4; | ||
679 | } | ||
680 | ", | ||
681 | ) | ||
682 | } | ||
661 | } | 683 | } |