@@ -57,7 +57,7 @@ pub(crate) fn replace_if_let_with_match(acc: &mut Assists, ctx: &AssistContext<'
5757 let if_exprs = successors ( Some ( if_expr. clone ( ) ) , |expr| match expr. else_branch ( ) ? {
5858 ast:: ElseBranch :: IfExpr ( expr) => Some ( expr) ,
5959 ast:: ElseBranch :: Block ( block) => {
60- let block = unwrap_trivial_block ( block) . clone_for_update ( ) ;
60+ let block = unwrap_trivial_block ( block) ;
6161 else_block = Some ( block. reset_indent ( ) . indent ( IndentLevel ( 1 ) ) ) ;
6262 None
6363 }
@@ -91,7 +91,7 @@ pub(crate) fn replace_if_let_with_match(acc: &mut Assists, ctx: &AssistContext<'
9191 guard
9292 } ;
9393
94- let body = if_expr. then_branch ( ) ?. clone_for_update ( ) . indent ( IndentLevel ( 1 ) ) ;
94+ let body = if_expr. then_branch ( ) ?. indent ( IndentLevel ( 1 ) ) ;
9595 cond_bodies. push ( ( cond, guard, body) ) ;
9696 }
9797
@@ -114,7 +114,7 @@ pub(crate) fn replace_if_let_with_match(acc: &mut Assists, ctx: &AssistContext<'
114114 let make_match_arm =
115115 |( pat, guard, body) : ( _ , Option < ast:: Expr > , ast:: BlockExpr ) | {
116116 // Dedent from original position, then indent for match arm
117- let body = body. dedent ( indent) . indent ( IndentLevel :: single ( ) ) ;
117+ let body = body. dedent ( indent) ;
118118 let body = unwrap_trivial_block ( body) ;
119119 match ( pat, guard. map ( |it| make. match_guard ( it) ) ) {
120120 ( Some ( pat) , guard) => make. match_arm ( pat, guard, body) ,
@@ -127,8 +127,8 @@ pub(crate) fn replace_if_let_with_match(acc: &mut Assists, ctx: &AssistContext<'
127127 }
128128 } ;
129129 let arms = cond_bodies. into_iter ( ) . map ( make_match_arm) . chain ( [ else_arm] ) ;
130- let match_expr =
131- make. expr_match ( scrutinee_to_be_expr , make. match_arm_list ( arms) ) . indent ( indent) ;
130+ let expr = scrutinee_to_be_expr . reset_indent ( ) ;
131+ let match_expr = make. expr_match ( expr , make. match_arm_list ( arms) ) . indent ( indent) ;
132132 match_expr. into ( )
133133 } ;
134134
@@ -246,7 +246,7 @@ pub(crate) fn replace_match_with_if_let(acc: &mut Assists, ctx: &AssistContext<'
246246 first_arm. guard ( ) ,
247247 second_arm. guard ( ) ,
248248 ) ?;
249- let scrutinee = match_expr. expr ( ) ?;
249+ let scrutinee = match_expr. expr ( ) ?. reset_indent ( ) ;
250250 let guard = guard. and_then ( |it| it. condition ( ) ) ;
251251
252252 let let_ = match & if_let_pat {
@@ -293,10 +293,8 @@ pub(crate) fn replace_match_with_if_let(acc: &mut Assists, ctx: &AssistContext<'
293293 } else {
294294 condition
295295 } ;
296- let then_expr =
297- then_expr. clone_for_update ( ) . reset_indent ( ) . indent ( IndentLevel :: single ( ) ) ;
298- let else_expr =
299- else_expr. clone_for_update ( ) . reset_indent ( ) . indent ( IndentLevel :: single ( ) ) ;
296+ let then_expr = then_expr. reset_indent ( ) ;
297+ let else_expr = else_expr. reset_indent ( ) ;
300298 let then_block = make_block_expr ( then_expr) ;
301299 let else_expr = if is_empty_expr ( & else_expr) { None } else { Some ( else_expr) } ;
302300 let if_let_expr = make
@@ -956,7 +954,9 @@ fn foo(x: Result<i32, ()>) {
956954 r#"
957955fn main() {
958956 if true {
959- $0if let Ok(rel_path) = path.strip_prefix(root_path) {
957+ $0if let Ok(rel_path) = path.strip_prefix(root_path)
958+ .and(x)
959+ {
960960 let rel_path = RelativePathBuf::from_path(rel_path)
961961 .ok()?;
962962 Some((*id, rel_path))
@@ -971,7 +971,8 @@ fn main() {
971971 r#"
972972fn main() {
973973 if true {
974- match path.strip_prefix(root_path) {
974+ match path.strip_prefix(root_path)
975+ .and(x) {
975976 Ok(rel_path) => {
976977 let rel_path = RelativePathBuf::from_path(rel_path)
977978 .ok()?;
@@ -993,7 +994,9 @@ fn main() {
993994 r#"
994995fn main() {
995996 if true {
996- $0if let Ok(rel_path) = path.strip_prefix(root_path) {
997+ $0if let Ok(rel_path) = path.strip_prefix(root_path)
998+ .and(x)
999+ {
9971000 Foo {
9981001 x: 1
9991002 }
@@ -1008,7 +1011,8 @@ fn main() {
10081011 r#"
10091012fn main() {
10101013 if true {
1011- match path.strip_prefix(root_path) {
1014+ match path.strip_prefix(root_path)
1015+ .and(x) {
10121016 Ok(rel_path) => {
10131017 Foo {
10141018 x: 1
@@ -1023,7 +1027,33 @@ fn main() {
10231027 }
10241028}
10251029"# ,
1026- )
1030+ ) ;
1031+
1032+ check_assist (
1033+ replace_if_let_with_match,
1034+ r#"
1035+ fn main() {
1036+ if true {
1037+ $0if true
1038+ && false
1039+ {
1040+ foo()
1041+ }
1042+ }
1043+ }
1044+ "# ,
1045+ r#"
1046+ fn main() {
1047+ if true {
1048+ match true
1049+ && false {
1050+ true => foo(),
1051+ false => (),
1052+ }
1053+ }
1054+ }
1055+ "# ,
1056+ ) ;
10271057 }
10281058
10291059 #[ test]
@@ -1878,7 +1908,9 @@ fn foo(x: Result<i32, ()>) {
18781908 r#"
18791909fn main() {
18801910 if true {
1881- $0match path.strip_prefix(root_path) {
1911+ $0match path.strip_prefix(root_path)
1912+ .and(x)
1913+ {
18821914 Ok(rel_path) => Foo {
18831915 x: 2
18841916 }
@@ -1892,7 +1924,8 @@ fn main() {
18921924 r#"
18931925fn main() {
18941926 if true {
1895- if let Ok(rel_path) = path.strip_prefix(root_path) {
1927+ if let Ok(rel_path) = path.strip_prefix(root_path)
1928+ .and(x) {
18961929 Foo {
18971930 x: 2
18981931 }
@@ -1911,7 +1944,9 @@ fn main() {
19111944 r#"
19121945fn main() {
19131946 if true {
1914- $0match path.strip_prefix(root_path) {
1947+ $0match path.strip_prefix(root_path)
1948+ .and(x)
1949+ {
19151950 Ok(rel_path) => {
19161951 let rel_path = RelativePathBuf::from_path(rel_path)
19171952 .ok()?;
@@ -1929,7 +1964,8 @@ fn main() {
19291964 r#"
19301965fn main() {
19311966 if true {
1932- if let Ok(rel_path) = path.strip_prefix(root_path) {
1967+ if let Ok(rel_path) = path.strip_prefix(root_path)
1968+ .and(x) {
19331969 let rel_path = RelativePathBuf::from_path(rel_path)
19341970 .ok()?;
19351971 Some((*id, rel_path))
0 commit comments