Skip to content

Don't show extract into variable assist for unit expressions #9108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 2, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions crates/ide_assists/src/handlers/extract_variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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
}
"#,
)
}
Expand Down Expand Up @@ -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() {
Expand Down