Skip to content

Commit 2bd2960

Browse files
committed
Don't support if-let branches
1 parent d0986cd commit 2bd2960

File tree

1 file changed

+22
-64
lines changed

1 file changed

+22
-64
lines changed

crates/ide_assists/src/handlers/move_guard.rs

Lines changed: 22 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -158,75 +158,51 @@ pub(crate) fn move_arm_cond_to_match_guard(acc: &mut Assists, ctx: &AssistContex
158158
}
159159
}
160160
}
161-
match tail {
162-
Some(Tail::IfLet(e)) => {
163-
cov_mark::hit!(move_guard_ifelse_iflet_tail);
164-
let guard = format!("\n{}{} => ", spaces, match_pat);
165-
// Put the if-let expression in a block
166-
let iflet_expr: Expr = e.reset_indent().indent(1.into()).into();
167-
let iflet_block =
168-
make::block_expr(std::iter::empty(), Some(iflet_expr)).indent(indent_level);
169-
edit.insert(then_arm_end, guard);
170-
edit.insert(then_arm_end, iflet_block.syntax().text());
171-
}
172-
Some(Tail::Else(e)) => {
173-
cov_mark::hit!(move_guard_ifelse_else_tail);
174-
let guard = format!("\n{}{} => ", spaces, match_pat);
175-
edit.insert(then_arm_end, guard);
176-
let only_expr = e.statements().next().is_none();
177-
match &e.tail_expr() {
178-
Some(expr) if only_expr => {
179-
cov_mark::hit!(move_guard_ifelse_expr_only);
180-
edit.insert(then_arm_end, expr.syntax().text());
181-
edit.insert(then_arm_end, ",");
182-
}
183-
_ => {
184-
let to_insert = e.dedent(dedent.into()).syntax().text();
185-
edit.insert(then_arm_end, to_insert)
186-
}
161+
if let Some(e) = tail {
162+
cov_mark::hit!(move_guard_ifelse_else_tail);
163+
let guard = format!("\n{}{} => ", spaces, match_pat);
164+
edit.insert(then_arm_end, guard);
165+
let only_expr = e.statements().next().is_none();
166+
match &e.tail_expr() {
167+
Some(expr) if only_expr => {
168+
cov_mark::hit!(move_guard_ifelse_expr_only);
169+
edit.insert(then_arm_end, expr.syntax().text());
170+
edit.insert(then_arm_end, ",");
171+
}
172+
_ => {
173+
let to_insert = e.dedent(dedent.into()).syntax().text();
174+
edit.insert(then_arm_end, to_insert)
187175
}
188176
}
189-
_ => {
190-
cov_mark::hit!(move_guard_ifelse_notail);
191-
}
177+
} else {
178+
cov_mark::hit!(move_guard_ifelse_notail);
192179
}
193180
},
194181
)
195182
}
196183

197-
#[derive(Debug)]
198-
enum Tail {
199-
Else(BlockExpr),
200-
IfLet(IfExpr),
201-
}
202-
203184
// Parses an if-else-if chain to get the conditons and the then branches until we encounter an else
204185
// branch, an if-let branch or the end.
205-
fn parse_if_chain(if_expr: IfExpr) -> Option<(Vec<(Condition, BlockExpr)>, Option<Tail>)> {
186+
fn parse_if_chain(if_expr: IfExpr) -> Option<(Vec<(Condition, BlockExpr)>, Option<BlockExpr>)> {
206187
let mut conds_blocks = Vec::new();
207188
let mut curr_if = if_expr;
208-
let mut applicable = false;
209-
let tail: Option<Tail> = loop {
189+
let tail = loop {
210190
let cond = curr_if.condition()?;
191+
// Not support moving if let to arm guard
211192
if cond.is_pattern_cond() {
212-
break Some(Tail::IfLet(curr_if));
193+
return None;
213194
}
214195
conds_blocks.push((cond, curr_if.then_branch()?));
215-
applicable = true;
216196
match curr_if.else_branch() {
217197
Some(ElseBranch::IfExpr(e)) => {
218198
curr_if = e;
219199
}
220200
Some(ElseBranch::Block(b)) => {
221-
break Some(Tail::Else(b));
201+
break Some(b);
222202
}
223203
None => break None,
224204
}
225205
};
226-
if !applicable {
227-
// The first if branch is an if-let branch
228-
return None;
229-
}
230206
Some((conds_blocks, tail))
231207
}
232208

@@ -853,8 +829,7 @@ fn main() {
853829

854830
#[test]
855831
fn move_arm_cond_to_match_guard_elseif_iflet() {
856-
cov_mark::check!(move_guard_ifelse_iflet_tail);
857-
check_assist(
832+
check_assist_not_applicable(
858833
move_arm_cond_to_match_guard,
859834
r#"
860835
fn main() {
@@ -872,23 +847,6 @@ fn main() {
872847
},
873848
}
874849
}
875-
"#,
876-
r#"
877-
fn main() {
878-
match 92 {
879-
3 => 0,
880-
x if x > 10 => 1,
881-
x if x > 5 => 2,
882-
x => {
883-
if let 4 = 4 {
884-
42;
885-
3
886-
} else {
887-
4
888-
}
889-
}
890-
}
891-
}
892850
"#,
893851
)
894852
}

0 commit comments

Comments
 (0)