diff options
-rw-r--r-- | crates/ide_assists/src/handlers/extract_variable.rs | 33 |
1 files changed, 31 insertions, 2 deletions
diff --git a/crates/ide_assists/src/handlers/extract_variable.rs b/crates/ide_assists/src/handlers/extract_variable.rs index ae084c86c..46b54a5f5 100644 --- a/crates/ide_assists/src/handlers/extract_variable.rs +++ b/crates/ide_assists/src/handlers/extract_variable.rs | |||
@@ -36,6 +36,11 @@ pub(crate) fn extract_variable(acc: &mut Assists, ctx: &AssistContext) -> Option | |||
36 | return None; | 36 | return None; |
37 | } | 37 | } |
38 | let to_extract = node.ancestors().find_map(valid_target_expr)?; | 38 | let to_extract = node.ancestors().find_map(valid_target_expr)?; |
39 | if let Some(ty) = ctx.sema.type_of_expr(&to_extract) { | ||
40 | if ty.is_unit() { | ||
41 | return None; | ||
42 | } | ||
43 | } | ||
39 | let anchor = Anchor::from(&to_extract)?; | 44 | let anchor = Anchor::from(&to_extract)?; |
40 | let indent = anchor.syntax().prev_sibling_or_token()?.as_token()?.clone(); | 45 | let indent = anchor.syntax().prev_sibling_or_token()?.as_token()?.clone(); |
41 | let target = to_extract.syntax().text_range(); | 46 | let target = to_extract.syntax().text_range(); |
@@ -275,15 +280,23 @@ fn foo() { | |||
275 | check_assist( | 280 | check_assist( |
276 | extract_variable, | 281 | extract_variable, |
277 | r#" | 282 | r#" |
278 | fn foo() { | 283 | fn foo() -> i32 { |
279 | $0bar(1 + 1)$0 | 284 | $0bar(1 + 1)$0 |
280 | } | 285 | } |
286 | |||
287 | fn bar(i: i32) -> i32 { | ||
288 | i | ||
289 | } | ||
281 | "#, | 290 | "#, |
282 | r#" | 291 | r#" |
283 | fn foo() { | 292 | fn foo() -> i32 { |
284 | let $0bar = bar(1 + 1); | 293 | let $0bar = bar(1 + 1); |
285 | bar | 294 | bar |
286 | } | 295 | } |
296 | |||
297 | fn bar(i: i32) -> i32 { | ||
298 | i | ||
299 | } | ||
287 | "#, | 300 | "#, |
288 | ) | 301 | ) |
289 | } | 302 | } |
@@ -796,6 +809,22 @@ fn foo() { | |||
796 | check_assist_not_applicable(extract_variable, "fn main() { loop { $0break$0; }; }"); | 809 | check_assist_not_applicable(extract_variable, "fn main() { loop { $0break$0; }; }"); |
797 | } | 810 | } |
798 | 811 | ||
812 | #[test] | ||
813 | fn test_extract_var_unit_expr_not_applicable() { | ||
814 | check_assist_not_applicable( | ||
815 | extract_variable, | ||
816 | r#" | ||
817 | fn foo() { | ||
818 | let mut i = 3; | ||
819 | $0if i >= 0 { | ||
820 | i += 1; | ||
821 | } else { | ||
822 | i -= 1; | ||
823 | }$0 | ||
824 | }"#, | ||
825 | ); | ||
826 | } | ||
827 | |||
799 | // FIXME: This is not quite correct, but good enough(tm) for the sorting heuristic | 828 | // FIXME: This is not quite correct, but good enough(tm) for the sorting heuristic |
800 | #[test] | 829 | #[test] |
801 | fn extract_var_target() { | 830 | fn extract_var_target() { |