Skip to content

Commit

Permalink
Auto merge of rust-lang#13726 - feniljain:fix_assists, r=jonas-schievink
Browse files Browse the repository at this point in the history
feat: allow unwrap block in let initializers

Possible fix for rust-lang#13679

### Points to help in review:

- I just added a parent case for let statements and it seems everything else was in place already, so turned out to be a small fix
  • Loading branch information
bors committed Dec 12, 2022
2 parents 3a7215b + 4794572 commit e7dff74
Showing 1 changed file with 46 additions and 1 deletion.
47 changes: 46 additions & 1 deletion crates/ide-assists/src/handlers/unwrap_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ pub(crate) fn unwrap_block(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option
parent = parent.ancestors().find(|it| ast::MatchExpr::can_cast(it.kind()))?
}

if matches!(parent.kind(), SyntaxKind::STMT_LIST | SyntaxKind::EXPR_STMT) {
if matches!(parent.kind(), SyntaxKind::STMT_LIST | SyntaxKind::EXPR_STMT | SyntaxKind::LET_STMT)
{
return acc.add(assist_id, assist_label, target, |builder| {
builder.replace(block.syntax().text_range(), update_expr_string(block.to_string()));
});
Expand Down Expand Up @@ -713,6 +714,50 @@ fn main() -> i32 {
return 3;
5
}
"#,
);
}

#[test]
fn unwrap_block_in_let_initializers() {
// https://github.com/rust-lang/rust-analyzer/issues/13679
check_assist(
unwrap_block,
r#"
fn main() {
let x = {$0
bar
};
}
"#,
r#"
fn main() {
let x = bar;
}
"#,
);
}

#[test]
fn unwrap_if_in_let_initializers() {
// https://github.com/rust-lang/rust-analyzer/issues/13679
check_assist(
unwrap_block,
r#"
fn main() {
let a = 1;
let x = if a - 1 == 0 {$0
foo
} else {
bar
};
}
"#,
r#"
fn main() {
let a = 1;
let x = foo;
}
"#,
);
}
Expand Down

0 comments on commit e7dff74

Please sign in to comment.