Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/ide-assists/src/handlers/convert_let_else_to_match.rs')
-rw-r--r--crates/ide-assists/src/handlers/convert_let_else_to_match.rs89
1 files changed, 83 insertions, 6 deletions
diff --git a/crates/ide-assists/src/handlers/convert_let_else_to_match.rs b/crates/ide-assists/src/handlers/convert_let_else_to_match.rs
index d2336a4a5d..9a9808e270 100644
--- a/crates/ide-assists/src/handlers/convert_let_else_to_match.rs
+++ b/crates/ide-assists/src/handlers/convert_let_else_to_match.rs
@@ -32,10 +32,12 @@ pub(crate) fn convert_let_else_to_match(acc: &mut Assists, ctx: &AssistContext<'
.or_else(|| ctx.find_token_syntax_at_offset(T![let])?.parent())?;
let let_stmt = LetStmt::cast(let_stmt)?;
let else_block = let_stmt.let_else()?.block_expr()?;
- let else_expr = if else_block.statements().next().is_none() {
- else_block.tail_expr()?
+ let else_expr = if else_block.statements().next().is_none()
+ && let Some(tail_expr) = else_block.tail_expr()
+ {
+ tail_expr.reset_indent()
} else {
- else_block.into()
+ else_block.reset_indent().into()
};
let init = let_stmt.initializer()?;
// Ignore let stmt with type annotation
@@ -91,8 +93,8 @@ pub(crate) fn convert_let_else_to_match(acc: &mut Assists, ctx: &AssistContext<'
},
);
let else_arm = make.match_arm(make.wildcard_pat().into(), None, else_expr);
- let match_ = make.expr_match(init, make.match_arm_list([binding_arm, else_arm]));
- let match_ = match_.reset_indent();
+ let arms = [binding_arm, else_arm].map(|arm| arm.indent(1.into()));
+ let match_ = make.expr_match(init, make.match_arm_list(arms));
let match_ = match_.indent(let_stmt.indent_level());
if bindings.is_empty() {
@@ -190,7 +192,7 @@ fn remove_mut_and_collect_idents(
let inner = p.pat()?;
if let ast::Pat::IdentPat(ident) = inner {
acc.push(ident);
- p.clone_for_update().into()
+ p.clone().into()
} else {
make.ref_pat(remove_mut_and_collect_idents(make, &inner, acc)?).into()
}
@@ -299,6 +301,81 @@ fn main() {
}
#[test]
+ fn convert_let_else_to_match_with_empty_else_block() {
+ check_assist(
+ convert_let_else_to_match,
+ r"
+fn main() {
+ let Ok(x) = f() else$0 {};
+}",
+ r"
+fn main() {
+ let x = match f() {
+ Ok(x) => x,
+ _ => {}
+ };
+}",
+ );
+ }
+
+ #[test]
+ fn convert_let_else_to_match_with_some_indent() {
+ check_assist(
+ convert_let_else_to_match,
+ r#"
+mod indent {
+ fn main() {
+ let Ok(x) = f() else$0 {
+ log();
+ unreachable!(
+ "..."
+ );
+ };
+ }
+}"#,
+ r#"
+mod indent {
+ fn main() {
+ let x = match f() {
+ Ok(x) => x,
+ _ => {
+ log();
+ unreachable!(
+ "..."
+ );
+ }
+ };
+ }
+}"#,
+ );
+
+ check_assist(
+ convert_let_else_to_match,
+ r#"
+mod indent {
+ fn main() {
+ let Ok(x) = f() else$0 {
+ unreachable!(
+ "..."
+ )
+ };
+ }
+}"#,
+ r#"
+mod indent {
+ fn main() {
+ let x = match f() {
+ Ok(x) => x,
+ _ => unreachable!(
+ "..."
+ ),
+ };
+ }
+}"#,
+ );
+ }
+
+ #[test]
fn convert_let_else_to_match_const_ref() {
check_assist(
convert_let_else_to_match,