@@ -30,7 +30,7 @@ use crate::{
30
30
// ```
31
31
pub ( crate ) fn convert_match_to_let_else ( acc : & mut Assists , ctx : & AssistContext < ' _ > ) -> Option < ( ) > {
32
32
let let_stmt: ast:: LetStmt = ctx. find_node_at_offset ( ) ?;
33
- let binding = find_binding ( let_stmt. pat ( ) ? ) ?;
33
+ let binding = let_stmt. pat ( ) ?;
34
34
35
35
let initializer = match let_stmt. initializer ( ) {
36
36
Some ( ast:: Expr :: MatchExpr ( it) ) => it,
@@ -47,7 +47,12 @@ pub(crate) fn convert_match_to_let_else(acc: &mut Assists, ctx: &AssistContext<'
47
47
return None ;
48
48
}
49
49
50
- let diverging_arm_expr = diverging_arm. expr ( ) ?;
50
+ let diverging_arm_expr = match diverging_arm. expr ( ) ? {
51
+ ast:: Expr :: BlockExpr ( block) if block. modifier ( ) . is_none ( ) && block. label ( ) . is_none ( ) => {
52
+ block. to_string ( )
53
+ }
54
+ other => format ! ( "{{ {other} }}" ) ,
55
+ } ;
51
56
let extracting_arm_pat = extracting_arm. pat ( ) ?;
52
57
let extracted_variable = find_extracted_variable ( ctx, & extracting_arm) ?;
53
58
@@ -56,24 +61,16 @@ pub(crate) fn convert_match_to_let_else(acc: &mut Assists, ctx: &AssistContext<'
56
61
"Convert match to let-else" ,
57
62
let_stmt. syntax ( ) . text_range ( ) ,
58
63
|builder| {
59
- let extracting_arm_pat = rename_variable ( & extracting_arm_pat, extracted_variable, binding) ;
64
+ let extracting_arm_pat =
65
+ rename_variable ( & extracting_arm_pat, extracted_variable, binding) ;
60
66
builder. replace (
61
67
let_stmt. syntax ( ) . text_range ( ) ,
62
- format ! ( "let {extracting_arm_pat} = {initializer_expr} else {{ { diverging_arm_expr} }} ;" )
68
+ format ! ( "let {extracting_arm_pat} = {initializer_expr} else {diverging_arm_expr};" ) ,
63
69
)
64
70
} ,
65
71
)
66
72
}
67
73
68
- // Given a pattern, find the name introduced to the surrounding scope.
69
- fn find_binding ( pat : ast:: Pat ) -> Option < ast:: IdentPat > {
70
- if let ast:: Pat :: IdentPat ( ident) = pat {
71
- Some ( ident)
72
- } else {
73
- None
74
- }
75
- }
76
-
77
74
// Given a match expression, find extracting and diverging arms.
78
75
fn find_arms (
79
76
ctx : & AssistContext < ' _ > ,
@@ -124,7 +121,7 @@ fn find_extracted_variable(ctx: &AssistContext<'_>, arm: &ast::MatchArm) -> Opti
124
121
}
125
122
126
123
// Rename `extracted` with `binding` in `pat`.
127
- fn rename_variable ( pat : & ast:: Pat , extracted : ast:: Name , binding : ast:: IdentPat ) -> SyntaxNode {
124
+ fn rename_variable ( pat : & ast:: Pat , extracted : ast:: Name , binding : ast:: Pat ) -> SyntaxNode {
128
125
let syntax = pat. syntax ( ) . clone_for_update ( ) ;
129
126
let extracted_syntax = syntax. covering_element ( extracted. syntax ( ) . text_range ( ) ) ;
130
127
@@ -136,7 +133,7 @@ fn rename_variable(pat: &ast::Pat, extracted: ast::Name, binding: ast::IdentPat)
136
133
if let Some ( name_ref) = record_pat_field. field_name ( ) {
137
134
ted:: replace (
138
135
record_pat_field. syntax ( ) ,
139
- ast:: make:: record_pat_field ( ast:: make:: name_ref ( & name_ref. text ( ) ) , binding. into ( ) )
136
+ ast:: make:: record_pat_field ( ast:: make:: name_ref ( & name_ref. text ( ) ) , binding)
140
137
. syntax ( )
141
138
. clone_for_update ( ) ,
142
139
) ;
@@ -410,4 +407,52 @@ fn foo(opt: Option<i32>) -> Option<i32> {
410
407
"# ,
411
408
) ;
412
409
}
410
+
411
+ #[ test]
412
+ fn complex_pattern ( ) {
413
+ check_assist (
414
+ convert_match_to_let_else,
415
+ r#"
416
+ //- minicore: option
417
+ fn f() {
418
+ let (x, y) = $0match Some((0, 1)) {
419
+ Some(it) => it,
420
+ None => return,
421
+ };
422
+ }
423
+ "# ,
424
+ r#"
425
+ fn f() {
426
+ let Some((x, y)) = Some((0, 1)) else { return };
427
+ }
428
+ "# ,
429
+ ) ;
430
+ }
431
+
432
+ #[ test]
433
+ fn diverging_block ( ) {
434
+ check_assist (
435
+ convert_match_to_let_else,
436
+ r#"
437
+ //- minicore: option
438
+ fn f() {
439
+ let x = $0match Some(()) {
440
+ Some(it) => it,
441
+ None => {//comment
442
+ println!("nope");
443
+ return
444
+ },
445
+ };
446
+ }
447
+ "# ,
448
+ r#"
449
+ fn f() {
450
+ let Some(x) = Some(()) else {//comment
451
+ println!("nope");
452
+ return
453
+ };
454
+ }
455
+ "# ,
456
+ ) ;
457
+ }
413
458
}
0 commit comments