aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-06-02 18:25:11 +0100
committerGitHub <[email protected]>2021-06-02 18:25:11 +0100
commit5be653d426e3e3fd253f41f85e7d280a82037da9 (patch)
tree044781a4271b8175b76309c7bd7a6e376a4914ea /crates
parenta421482e7515414ae4e5e9f76bf997c7f1e7398b (diff)
parent7d2710218f176f0f8e2bb86288084559a358dc7d (diff)
Merge #9108
9108: Don't show extract into variable assist for unit expressions r=jonas-schievink a=brandondong **Reproduction:** ```rust fn main() { let mut i = 3; $0if i >= 0 { i += 1; } else { i -= 1; }$0 } ``` 1. Select the snippet of code between the $0's. 2. The extract into variable assist shows up, pushing down the more useful extract into function assist. 3. The resulting output of selecting the extract into variable assist is valid but with the extracted variable having the unit type: ```rust fn main() { let mut i = 3; let var_name = if i >= 0 { i += 1; } else { i -= 1; }; var_name } ``` **Fix:** - Don't show the extract into variable assist for unit expressions. I could not think of any scenarios where such a variable extraction would be desired. Co-authored-by: Brandon <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r--crates/ide_assists/src/handlers/extract_variable.rs33
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#"
278fn foo() { 283fn foo() -> i32 {
279 $0bar(1 + 1)$0 284 $0bar(1 + 1)$0
280} 285}
286
287fn bar(i: i32) -> i32 {
288 i
289}
281"#, 290"#,
282 r#" 291 r#"
283fn foo() { 292fn foo() -> i32 {
284 let $0bar = bar(1 + 1); 293 let $0bar = bar(1 + 1);
285 bar 294 bar
286} 295}
296
297fn 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#"
817fn 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() {