aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_assists
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide_assists')
-rw-r--r--crates/ide_assists/src/handlers/pull_assignment_up.rs52
1 files changed, 36 insertions, 16 deletions
diff --git a/crates/ide_assists/src/handlers/pull_assignment_up.rs b/crates/ide_assists/src/handlers/pull_assignment_up.rs
index 04bae4e58..543b1dfe9 100644
--- a/crates/ide_assists/src/handlers/pull_assignment_up.rs
+++ b/crates/ide_assists/src/handlers/pull_assignment_up.rs
@@ -37,22 +37,24 @@ use crate::{
37// ``` 37// ```
38pub(crate) fn pull_assignment_up(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { 38pub(crate) fn pull_assignment_up(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
39 let assign_expr = ctx.find_node_at_offset::<ast::BinExpr>()?; 39 let assign_expr = ctx.find_node_at_offset::<ast::BinExpr>()?;
40 let name_expr = if assign_expr.op_kind()? == ast::BinOp::Assignment { 40
41 assign_expr.lhs()? 41 let op_kind = assign_expr.op_kind()?;
42 } else { 42 if op_kind != ast::BinOp::Assignment {
43 cov_mark::hit!(test_cant_pull_non_assignments);
43 return None; 44 return None;
44 }; 45 }
45 46
46 let (old_stmt, new_stmt) = if let Some(if_expr) = ctx.find_node_at_offset::<ast::IfExpr>() { 47 let name_expr = assign_expr.lhs()?;
47 ( 48
48 ast::Expr::cast(if_expr.syntax().to_owned())?, 49 let old_stmt: ast::Expr;
49 exprify_if(&if_expr, &ctx.sema, &name_expr)?.indent(if_expr.indent_level()), 50 let new_stmt: ast::Expr;
50 ) 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();
51 } else if let Some(match_expr) = ctx.find_node_at_offset::<ast::MatchExpr>() { 55 } else if let Some(match_expr) = ctx.find_node_at_offset::<ast::MatchExpr>() {
52 ( 56 new_stmt = exprify_match(&match_expr, &ctx.sema, &name_expr)?;
53 ast::Expr::cast(match_expr.syntax().to_owned())?, 57 old_stmt = match_expr.into()
54 exprify_match(&match_expr, &ctx.sema, &name_expr)?,
55 )
56 } else { 58 } else {
57 return None; 59 return None;
58 }; 60 };
@@ -99,9 +101,7 @@ fn exprify_if(
99) -> Option<ast::Expr> { 101) -> Option<ast::Expr> {
100 let then_branch = exprify_block(&statement.then_branch()?, sema, name)?; 102 let then_branch = exprify_block(&statement.then_branch()?, sema, name)?;
101 let else_branch = match statement.else_branch()? { 103 let else_branch = match statement.else_branch()? {
102 ast::ElseBranch::Block(ref block) => { 104 ast::ElseBranch::Block(block) => ast::ElseBranch::Block(exprify_block(&block, sema, name)?),
103 ast::ElseBranch::Block(exprify_block(block, sema, name)?)
104 }
105 ast::ElseBranch::IfExpr(expr) => { 105 ast::ElseBranch::IfExpr(expr) => {
106 cov_mark::hit!(test_pull_assignment_up_chained_if); 106 cov_mark::hit!(test_pull_assignment_up_chained_if);
107 ast::ElseBranch::IfExpr(ast::IfExpr::cast( 107 ast::ElseBranch::IfExpr(ast::IfExpr::cast(
@@ -439,4 +439,24 @@ fn foo() {
439"#, 439"#,
440 ) 440 )
441 } 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#"
449fn 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}
459"#,
460 )
461 }
442} 462}