From 7d2710218f176f0f8e2bb86288084559a358dc7d Mon Sep 17 00:00:00 2001 From: Brandon Date: Tue, 1 Jun 2021 23:49:31 -0700 Subject: Don't show extract into variable assist for unit expressions --- .../ide_assists/src/handlers/extract_variable.rs | 33 ++++++++++++++++++++-- 1 file 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 return None; } let to_extract = node.ancestors().find_map(valid_target_expr)?; + if let Some(ty) = ctx.sema.type_of_expr(&to_extract) { + if ty.is_unit() { + return None; + } + } let anchor = Anchor::from(&to_extract)?; let indent = anchor.syntax().prev_sibling_or_token()?.as_token()?.clone(); let target = to_extract.syntax().text_range(); @@ -275,15 +280,23 @@ fn foo() { check_assist( extract_variable, r#" -fn foo() { +fn foo() -> i32 { $0bar(1 + 1)$0 } + +fn bar(i: i32) -> i32 { + i +} "#, r#" -fn foo() { +fn foo() -> i32 { let $0bar = bar(1 + 1); bar } + +fn bar(i: i32) -> i32 { + i +} "#, ) } @@ -796,6 +809,22 @@ fn foo() { check_assist_not_applicable(extract_variable, "fn main() { loop { $0break$0; }; }"); } + #[test] + fn test_extract_var_unit_expr_not_applicable() { + check_assist_not_applicable( + extract_variable, + r#" +fn foo() { + let mut i = 3; + $0if i >= 0 { + i += 1; + } else { + i -= 1; + }$0 +}"#, + ); + } + // FIXME: This is not quite correct, but good enough(tm) for the sorting heuristic #[test] fn extract_var_target() { -- cgit v1.2.3