Skip to content

Commit e603090

Browse files
committed
minor: add missing test
1 parent 880dddd commit e603090

File tree

1 file changed

+36
-16
lines changed

1 file changed

+36
-16
lines changed

crates/ide_assists/src/handlers/pull_assignment_up.rs

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,24 @@ use crate::{
3737
// ```
3838
pub(crate) fn pull_assignment_up(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
3939
let assign_expr = ctx.find_node_at_offset::<ast::BinExpr>()?;
40-
let name_expr = if assign_expr.op_kind()? == ast::BinOp::Assignment {
41-
assign_expr.lhs()?
42-
} else {
40+
41+
let op_kind = assign_expr.op_kind()?;
42+
if op_kind != ast::BinOp::Assignment {
43+
cov_mark::hit!(test_cant_pull_non_assignments);
4344
return None;
44-
};
45+
}
4546

46-
let (old_stmt, new_stmt) = if let Some(if_expr) = ctx.find_node_at_offset::<ast::IfExpr>() {
47-
(
48-
ast::Expr::cast(if_expr.syntax().to_owned())?,
49-
exprify_if(&if_expr, &ctx.sema, &name_expr)?.indent(if_expr.indent_level()),
50-
)
47+
let name_expr = assign_expr.lhs()?;
48+
49+
let old_stmt: ast::Expr;
50+
let new_stmt: ast::Expr;
51+
52+
if let Some(if_expr) = ctx.find_node_at_offset::<ast::IfExpr>() {
53+
new_stmt = exprify_if(&if_expr, &ctx.sema, &name_expr)?.indent(if_expr.indent_level());
54+
old_stmt = if_expr.into();
5155
} else if let Some(match_expr) = ctx.find_node_at_offset::<ast::MatchExpr>() {
52-
(
53-
ast::Expr::cast(match_expr.syntax().to_owned())?,
54-
exprify_match(&match_expr, &ctx.sema, &name_expr)?,
55-
)
56+
new_stmt = exprify_match(&match_expr, &ctx.sema, &name_expr)?;
57+
old_stmt = match_expr.into()
5658
} else {
5759
return None;
5860
};
@@ -99,9 +101,7 @@ fn exprify_if(
99101
) -> Option<ast::Expr> {
100102
let then_branch = exprify_block(&statement.then_branch()?, sema, name)?;
101103
let else_branch = match statement.else_branch()? {
102-
ast::ElseBranch::Block(ref block) => {
103-
ast::ElseBranch::Block(exprify_block(block, sema, name)?)
104-
}
104+
ast::ElseBranch::Block(block) => ast::ElseBranch::Block(exprify_block(&block, sema, name)?),
105105
ast::ElseBranch::IfExpr(expr) => {
106106
cov_mark::hit!(test_pull_assignment_up_chained_if);
107107
ast::ElseBranch::IfExpr(ast::IfExpr::cast(
@@ -436,6 +436,26 @@ fn foo() {
436436
3
437437
};
438438
}
439+
"#,
440+
)
441+
}
442+
443+
#[test]
444+
fn test_cant_pull_non_assignments() {
445+
cov_mark::check!(test_cant_pull_non_assignments);
446+
check_assist_not_applicable(
447+
pull_assignment_up,
448+
r#"
449+
fn foo() {
450+
let mut a = 1;
451+
let b = &mut a;
452+
453+
if true {
454+
$0*b + 2;
455+
} else {
456+
*b + 3;
457+
}
458+
}
439459
"#,
440460
)
441461
}

0 commit comments

Comments
 (0)