diff options
Diffstat (limited to 'crates/ra_assists/src/handlers')
-rw-r--r-- | crates/ra_assists/src/handlers/inline_local_variable.rs | 30 |
1 files changed, 26 insertions, 4 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..98e5112df 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::*; |
@@ -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; |
@@ -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 | } |