From e603090961d950b1130950c179361b530c7ad10a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 8 May 2021 20:40:07 +0300 Subject: minor: add missing test --- .../ide_assists/src/handlers/pull_assignment_up.rs | 52 +++++++++++++++------- 1 file changed, 36 insertions(+), 16 deletions(-) (limited to 'crates') 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::{ // ``` pub(crate) fn pull_assignment_up(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { let assign_expr = ctx.find_node_at_offset::()?; - let name_expr = if assign_expr.op_kind()? == ast::BinOp::Assignment { - assign_expr.lhs()? - } else { + + let op_kind = assign_expr.op_kind()?; + if op_kind != ast::BinOp::Assignment { + cov_mark::hit!(test_cant_pull_non_assignments); return None; - }; + } - let (old_stmt, new_stmt) = if let Some(if_expr) = ctx.find_node_at_offset::() { - ( - ast::Expr::cast(if_expr.syntax().to_owned())?, - exprify_if(&if_expr, &ctx.sema, &name_expr)?.indent(if_expr.indent_level()), - ) + let name_expr = assign_expr.lhs()?; + + let old_stmt: ast::Expr; + let new_stmt: ast::Expr; + + if let Some(if_expr) = ctx.find_node_at_offset::() { + new_stmt = exprify_if(&if_expr, &ctx.sema, &name_expr)?.indent(if_expr.indent_level()); + old_stmt = if_expr.into(); } else if let Some(match_expr) = ctx.find_node_at_offset::() { - ( - ast::Expr::cast(match_expr.syntax().to_owned())?, - exprify_match(&match_expr, &ctx.sema, &name_expr)?, - ) + new_stmt = exprify_match(&match_expr, &ctx.sema, &name_expr)?; + old_stmt = match_expr.into() } else { return None; }; @@ -99,9 +101,7 @@ fn exprify_if( ) -> Option { let then_branch = exprify_block(&statement.then_branch()?, sema, name)?; let else_branch = match statement.else_branch()? { - ast::ElseBranch::Block(ref block) => { - ast::ElseBranch::Block(exprify_block(block, sema, name)?) - } + ast::ElseBranch::Block(block) => ast::ElseBranch::Block(exprify_block(&block, sema, name)?), ast::ElseBranch::IfExpr(expr) => { cov_mark::hit!(test_pull_assignment_up_chained_if); ast::ElseBranch::IfExpr(ast::IfExpr::cast( @@ -436,6 +436,26 @@ fn foo() { 3 }; } +"#, + ) + } + + #[test] + fn test_cant_pull_non_assignments() { + cov_mark::check!(test_cant_pull_non_assignments); + check_assist_not_applicable( + pull_assignment_up, + r#" +fn foo() { + let mut a = 1; + let b = &mut a; + + if true { + $0*b + 2; + } else { + *b + 3; + } +} "#, ) } -- cgit v1.2.3