2021-01-01 18:55:56 -06:00
|
|
|
use syntax::{
|
2021-05-08 15:09:36 -05:00
|
|
|
ast::{self, make},
|
|
|
|
ted, AstNode,
|
2021-01-01 18:55:56 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
assist_context::{AssistContext, Assists},
|
|
|
|
AssistId, AssistKind,
|
|
|
|
};
|
|
|
|
|
2021-01-04 07:38:34 -06:00
|
|
|
// Assist: pull_assignment_up
|
2021-01-01 18:55:56 -06:00
|
|
|
//
|
2021-01-04 07:38:34 -06:00
|
|
|
// Extracts variable assignment to outside an if or match statement.
|
2021-01-01 18:55:56 -06:00
|
|
|
//
|
|
|
|
// ```
|
|
|
|
// fn main() {
|
|
|
|
// let mut foo = 6;
|
|
|
|
//
|
|
|
|
// if true {
|
2021-01-06 14:15:48 -06:00
|
|
|
// $0foo = 5;
|
2021-01-01 18:55:56 -06:00
|
|
|
// } else {
|
|
|
|
// foo = 4;
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
|
|
|
// fn main() {
|
|
|
|
// let mut foo = 6;
|
|
|
|
//
|
|
|
|
// foo = if true {
|
|
|
|
// 5
|
|
|
|
// } else {
|
|
|
|
// 4
|
|
|
|
// };
|
|
|
|
// }
|
|
|
|
// ```
|
2022-07-20 08:02:08 -05:00
|
|
|
pub(crate) fn pull_assignment_up(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
|
2021-01-03 07:11:42 -06:00
|
|
|
let assign_expr = ctx.find_node_at_offset::<ast::BinExpr>()?;
|
2021-05-08 12:40:07 -05:00
|
|
|
|
|
|
|
let op_kind = assign_expr.op_kind()?;
|
2021-08-14 10:08:31 -05:00
|
|
|
if op_kind != (ast::BinaryOp::Assignment { op: None }) {
|
2021-05-08 12:40:07 -05:00
|
|
|
cov_mark::hit!(test_cant_pull_non_assignments);
|
2021-01-03 07:11:42 -06:00
|
|
|
return None;
|
2021-05-08 12:40:07 -05:00
|
|
|
}
|
2021-01-01 18:55:56 -06:00
|
|
|
|
2021-05-08 15:09:36 -05:00
|
|
|
let mut collector = AssignmentsCollector {
|
|
|
|
sema: &ctx.sema,
|
|
|
|
common_lhs: assign_expr.lhs()?,
|
|
|
|
assignments: Vec::new(),
|
|
|
|
};
|
2021-05-08 12:40:07 -05:00
|
|
|
|
2021-05-08 15:09:36 -05:00
|
|
|
let tgt: ast::Expr = if let Some(if_expr) = ctx.find_node_at_offset::<ast::IfExpr>() {
|
|
|
|
collector.collect_if(&if_expr)?;
|
|
|
|
if_expr.into()
|
2021-01-02 08:33:23 -06:00
|
|
|
} else if let Some(match_expr) = ctx.find_node_at_offset::<ast::MatchExpr>() {
|
2021-05-08 15:09:36 -05:00
|
|
|
collector.collect_match(&match_expr)?;
|
|
|
|
match_expr.into()
|
2021-01-02 08:33:23 -06:00
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
};
|
2021-01-01 18:55:56 -06:00
|
|
|
|
2021-05-11 07:22:51 -05:00
|
|
|
if let Some(parent) = tgt.syntax().parent() {
|
|
|
|
if matches!(parent.kind(), syntax::SyntaxKind::BIN_EXPR | syntax::SyntaxKind::LET_STMT) {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-01 18:55:56 -06:00
|
|
|
acc.add(
|
2021-01-04 07:38:34 -06:00
|
|
|
AssistId("pull_assignment_up", AssistKind::RefactorExtract),
|
|
|
|
"Pull assignment up",
|
2021-05-08 15:09:36 -05:00
|
|
|
tgt.syntax().text_range(),
|
2021-01-01 18:55:56 -06:00
|
|
|
move |edit| {
|
2021-05-08 15:09:36 -05:00
|
|
|
let assignments: Vec<_> = collector
|
|
|
|
.assignments
|
|
|
|
.into_iter()
|
2021-05-16 06:18:49 -05:00
|
|
|
.map(|(stmt, rhs)| (edit.make_mut(stmt), rhs.clone_for_update()))
|
2021-05-08 15:09:36 -05:00
|
|
|
.collect();
|
|
|
|
|
2021-05-16 06:18:49 -05:00
|
|
|
let tgt = edit.make_mut(tgt);
|
2021-05-08 15:09:36 -05:00
|
|
|
|
|
|
|
for (stmt, rhs) in assignments {
|
2021-05-11 07:22:51 -05:00
|
|
|
let mut stmt = stmt.syntax().clone();
|
|
|
|
if let Some(parent) = stmt.parent() {
|
|
|
|
if ast::ExprStmt::cast(parent.clone()).is_some() {
|
|
|
|
stmt = parent.clone();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ted::replace(stmt, rhs.syntax());
|
2021-05-08 15:09:36 -05:00
|
|
|
}
|
|
|
|
let assign_expr = make::expr_assignment(collector.common_lhs, tgt.clone());
|
|
|
|
let assign_stmt = make::expr_stmt(assign_expr);
|
|
|
|
|
|
|
|
ted::replace(tgt.syntax(), assign_stmt.syntax().clone_for_update());
|
2021-01-01 18:55:56 -06:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-05-08 15:09:36 -05:00
|
|
|
struct AssignmentsCollector<'a> {
|
|
|
|
sema: &'a hir::Semantics<'a, ide_db::RootDatabase>,
|
|
|
|
common_lhs: ast::Expr,
|
2021-05-11 07:22:51 -05:00
|
|
|
assignments: Vec<(ast::BinExpr, ast::Expr)>,
|
2021-01-02 08:33:23 -06:00
|
|
|
}
|
|
|
|
|
2021-05-08 15:09:36 -05:00
|
|
|
impl<'a> AssignmentsCollector<'a> {
|
|
|
|
fn collect_match(&mut self, match_expr: &ast::MatchExpr) -> Option<()> {
|
|
|
|
for arm in match_expr.match_arm_list()?.arms() {
|
|
|
|
match arm.expr()? {
|
|
|
|
ast::Expr::BlockExpr(block) => self.collect_block(&block)?,
|
2021-05-11 07:22:51 -05:00
|
|
|
ast::Expr::BinExpr(expr) => self.collect_expr(&expr)?,
|
2021-05-08 15:09:36 -05:00
|
|
|
_ => return None,
|
|
|
|
}
|
2021-01-01 18:55:56 -06:00
|
|
|
}
|
|
|
|
|
2021-05-08 15:09:36 -05:00
|
|
|
Some(())
|
2021-01-01 18:55:56 -06:00
|
|
|
}
|
2021-05-08 15:09:36 -05:00
|
|
|
fn collect_if(&mut self, if_expr: &ast::IfExpr) -> Option<()> {
|
|
|
|
let then_branch = if_expr.then_branch()?;
|
|
|
|
self.collect_block(&then_branch)?;
|
2021-01-01 18:55:56 -06:00
|
|
|
|
2021-05-08 15:09:36 -05:00
|
|
|
match if_expr.else_branch()? {
|
|
|
|
ast::ElseBranch::Block(block) => self.collect_block(&block),
|
|
|
|
ast::ElseBranch::IfExpr(expr) => {
|
|
|
|
cov_mark::hit!(test_pull_assignment_up_chained_if);
|
|
|
|
self.collect_if(&expr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fn collect_block(&mut self, block: &ast::BlockExpr) -> Option<()> {
|
2021-10-03 07:53:01 -05:00
|
|
|
let last_expr = block.tail_expr().or_else(|| match block.statements().last()? {
|
|
|
|
ast::Stmt::ExprStmt(stmt) => stmt.expr(),
|
|
|
|
ast::Stmt::Item(_) | ast::Stmt::LetStmt(_) => None,
|
2021-05-11 07:22:51 -05:00
|
|
|
})?;
|
|
|
|
|
|
|
|
if let ast::Expr::BinExpr(expr) = last_expr {
|
|
|
|
return self.collect_expr(&expr);
|
2021-01-01 18:55:56 -06:00
|
|
|
}
|
2021-05-08 15:09:36 -05:00
|
|
|
|
|
|
|
None
|
2021-01-01 18:55:56 -06:00
|
|
|
}
|
2021-05-11 07:22:51 -05:00
|
|
|
|
|
|
|
fn collect_expr(&mut self, expr: &ast::BinExpr) -> Option<()> {
|
2021-08-14 10:08:31 -05:00
|
|
|
if expr.op_kind()? == (ast::BinaryOp::Assignment { op: None })
|
2021-05-11 07:22:51 -05:00
|
|
|
&& is_equivalent(self.sema, &expr.lhs()?, &self.common_lhs)
|
|
|
|
{
|
|
|
|
self.assignments.push((expr.clone(), expr.rhs()?));
|
|
|
|
return Some(());
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
2021-01-01 18:55:56 -06:00
|
|
|
}
|
|
|
|
|
2021-01-03 07:11:42 -06:00
|
|
|
fn is_equivalent(
|
2022-07-20 08:02:08 -05:00
|
|
|
sema: &hir::Semantics<'_, ide_db::RootDatabase>,
|
2021-01-03 07:11:42 -06:00
|
|
|
expr0: &ast::Expr,
|
|
|
|
expr1: &ast::Expr,
|
|
|
|
) -> bool {
|
|
|
|
match (expr0, expr1) {
|
|
|
|
(ast::Expr::FieldExpr(field_expr0), ast::Expr::FieldExpr(field_expr1)) => {
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::hit!(test_pull_assignment_up_field_assignment);
|
2021-01-03 07:11:42 -06:00
|
|
|
sema.resolve_field(field_expr0) == sema.resolve_field(field_expr1)
|
|
|
|
}
|
|
|
|
(ast::Expr::PathExpr(path0), ast::Expr::PathExpr(path1)) => {
|
|
|
|
let path0 = path0.path();
|
|
|
|
let path1 = path1.path();
|
|
|
|
if let (Some(path0), Some(path1)) = (path0, path1) {
|
|
|
|
sema.resolve_path(&path0) == sema.resolve_path(&path1)
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
2021-03-04 07:43:52 -06:00
|
|
|
(ast::Expr::PrefixExpr(prefix0), ast::Expr::PrefixExpr(prefix1))
|
2021-08-14 09:01:28 -05:00
|
|
|
if prefix0.op_kind() == Some(ast::UnaryOp::Deref)
|
|
|
|
&& prefix1.op_kind() == Some(ast::UnaryOp::Deref) =>
|
2021-03-04 07:43:52 -06:00
|
|
|
{
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::hit!(test_pull_assignment_up_deref);
|
2021-03-04 07:43:52 -06:00
|
|
|
if let (Some(prefix0), Some(prefix1)) = (prefix0.expr(), prefix1.expr()) {
|
|
|
|
is_equivalent(sema, &prefix0, &prefix1)
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
2021-01-03 07:11:42 -06:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-01 18:55:56 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
use crate::tests::{check_assist, check_assist_not_applicable};
|
|
|
|
|
|
|
|
#[test]
|
2021-01-04 07:38:34 -06:00
|
|
|
fn test_pull_assignment_up_if() {
|
2021-01-01 18:55:56 -06:00
|
|
|
check_assist(
|
2021-01-04 07:38:34 -06:00
|
|
|
pull_assignment_up,
|
2021-01-01 18:55:56 -06:00
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
let mut a = 1;
|
|
|
|
|
|
|
|
if true {
|
2021-01-06 14:15:48 -06:00
|
|
|
$0a = 2;
|
2021-01-01 18:55:56 -06:00
|
|
|
} else {
|
|
|
|
a = 3;
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
let mut a = 1;
|
|
|
|
|
|
|
|
a = if true {
|
|
|
|
2
|
|
|
|
} else {
|
|
|
|
3
|
|
|
|
};
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-01-02 08:33:23 -06:00
|
|
|
#[test]
|
2021-01-04 07:38:34 -06:00
|
|
|
fn test_pull_assignment_up_match() {
|
2021-01-02 08:33:23 -06:00
|
|
|
check_assist(
|
2021-01-04 07:38:34 -06:00
|
|
|
pull_assignment_up,
|
2021-01-02 08:33:23 -06:00
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
let mut a = 1;
|
|
|
|
|
|
|
|
match 1 {
|
|
|
|
1 => {
|
2021-01-06 14:15:48 -06:00
|
|
|
$0a = 2;
|
2021-01-02 08:33:23 -06:00
|
|
|
},
|
|
|
|
2 => {
|
|
|
|
a = 3;
|
|
|
|
},
|
|
|
|
3 => {
|
|
|
|
a = 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
let mut a = 1;
|
|
|
|
|
|
|
|
a = match 1 {
|
|
|
|
1 => {
|
|
|
|
2
|
|
|
|
},
|
|
|
|
2 => {
|
|
|
|
3
|
|
|
|
},
|
|
|
|
3 => {
|
|
|
|
4
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-05-08 15:19:08 -05:00
|
|
|
#[test]
|
|
|
|
fn test_pull_assignment_up_assignment_expressions() {
|
|
|
|
check_assist(
|
|
|
|
pull_assignment_up,
|
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
let mut a = 1;
|
|
|
|
|
|
|
|
match 1 {
|
|
|
|
1 => { $0a = 2; },
|
|
|
|
2 => a = 3,
|
|
|
|
3 => {
|
|
|
|
a = 4
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
let mut a = 1;
|
|
|
|
|
|
|
|
a = match 1 {
|
|
|
|
1 => { 2 },
|
|
|
|
2 => 3,
|
|
|
|
3 => {
|
|
|
|
4
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-01-01 18:55:56 -06:00
|
|
|
#[test]
|
2021-01-04 07:38:34 -06:00
|
|
|
fn test_pull_assignment_up_not_last_not_applicable() {
|
2021-01-01 18:55:56 -06:00
|
|
|
check_assist_not_applicable(
|
2021-01-04 07:38:34 -06:00
|
|
|
pull_assignment_up,
|
2021-01-01 18:55:56 -06:00
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
let mut a = 1;
|
|
|
|
|
|
|
|
if true {
|
2021-01-06 14:15:48 -06:00
|
|
|
$0a = 2;
|
2021-01-01 18:55:56 -06:00
|
|
|
b = a;
|
|
|
|
} else {
|
|
|
|
a = 3;
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-01-04 07:38:34 -06:00
|
|
|
fn test_pull_assignment_up_chained_if() {
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::check!(test_pull_assignment_up_chained_if);
|
2021-01-01 18:55:56 -06:00
|
|
|
check_assist(
|
2021-01-04 07:38:34 -06:00
|
|
|
pull_assignment_up,
|
2021-01-01 18:55:56 -06:00
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
let mut a = 1;
|
|
|
|
|
|
|
|
if true {
|
2021-01-06 14:15:48 -06:00
|
|
|
$0a = 2;
|
2021-01-01 18:55:56 -06:00
|
|
|
} else if false {
|
|
|
|
a = 3;
|
|
|
|
} else {
|
|
|
|
a = 4;
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
let mut a = 1;
|
|
|
|
|
|
|
|
a = if true {
|
|
|
|
2
|
|
|
|
} else if false {
|
|
|
|
3
|
|
|
|
} else {
|
|
|
|
4
|
|
|
|
};
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-01-04 07:38:34 -06:00
|
|
|
fn test_pull_assignment_up_retains_stmts() {
|
2021-01-01 18:55:56 -06:00
|
|
|
check_assist(
|
2021-01-04 07:38:34 -06:00
|
|
|
pull_assignment_up,
|
2021-01-01 18:55:56 -06:00
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
let mut a = 1;
|
|
|
|
|
|
|
|
if true {
|
|
|
|
let b = 2;
|
2021-01-06 14:15:48 -06:00
|
|
|
$0a = 2;
|
2021-01-01 18:55:56 -06:00
|
|
|
} else {
|
|
|
|
let b = 3;
|
|
|
|
a = 3;
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
let mut a = 1;
|
|
|
|
|
|
|
|
a = if true {
|
|
|
|
let b = 2;
|
|
|
|
2
|
|
|
|
} else {
|
|
|
|
let b = 3;
|
|
|
|
3
|
|
|
|
};
|
|
|
|
}"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-01-04 07:38:34 -06:00
|
|
|
fn pull_assignment_up_let_stmt_not_applicable() {
|
2021-01-01 18:55:56 -06:00
|
|
|
check_assist_not_applicable(
|
2021-01-04 07:38:34 -06:00
|
|
|
pull_assignment_up,
|
2021-01-01 18:55:56 -06:00
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
let mut a = 1;
|
|
|
|
|
|
|
|
let b = if true {
|
2021-01-06 14:15:48 -06:00
|
|
|
$0a = 2
|
2021-01-01 18:55:56 -06:00
|
|
|
} else {
|
|
|
|
a = 3
|
|
|
|
};
|
|
|
|
}"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-04-13 17:35:00 -05:00
|
|
|
fn pull_assignment_up_if_missing_assignment_not_applicable() {
|
2021-01-01 18:55:56 -06:00
|
|
|
check_assist_not_applicable(
|
2021-01-04 07:38:34 -06:00
|
|
|
pull_assignment_up,
|
2021-01-01 18:55:56 -06:00
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
let mut a = 1;
|
|
|
|
|
|
|
|
if true {
|
2021-01-06 14:15:48 -06:00
|
|
|
$0a = 2;
|
2021-01-01 18:55:56 -06:00
|
|
|
} else {}
|
2021-01-02 08:33:23 -06:00
|
|
|
}"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2023-04-13 17:35:00 -05:00
|
|
|
fn pull_assignment_up_match_missing_assignment_not_applicable() {
|
2021-01-02 08:33:23 -06:00
|
|
|
check_assist_not_applicable(
|
2021-01-04 07:38:34 -06:00
|
|
|
pull_assignment_up,
|
2021-01-02 08:33:23 -06:00
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
let mut a = 1;
|
|
|
|
|
|
|
|
match 1 {
|
|
|
|
1 => {
|
2021-01-06 14:15:48 -06:00
|
|
|
$0a = 2;
|
2021-01-02 08:33:23 -06:00
|
|
|
},
|
|
|
|
2 => {
|
|
|
|
a = 3;
|
|
|
|
},
|
|
|
|
3 => {},
|
|
|
|
}
|
2021-01-03 07:11:42 -06:00
|
|
|
}"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-01-04 07:38:34 -06:00
|
|
|
fn test_pull_assignment_up_field_assignment() {
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::check!(test_pull_assignment_up_field_assignment);
|
2021-01-03 07:11:42 -06:00
|
|
|
check_assist(
|
2021-01-04 07:38:34 -06:00
|
|
|
pull_assignment_up,
|
2021-01-03 07:11:42 -06:00
|
|
|
r#"
|
|
|
|
struct A(usize);
|
|
|
|
|
|
|
|
fn foo() {
|
|
|
|
let mut a = A(1);
|
|
|
|
|
|
|
|
if true {
|
2021-01-06 14:15:48 -06:00
|
|
|
$0a.0 = 2;
|
2021-01-03 07:11:42 -06:00
|
|
|
} else {
|
|
|
|
a.0 = 3;
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
r#"
|
|
|
|
struct A(usize);
|
|
|
|
|
|
|
|
fn foo() {
|
|
|
|
let mut a = A(1);
|
|
|
|
|
|
|
|
a.0 = if true {
|
|
|
|
2
|
|
|
|
} else {
|
|
|
|
3
|
|
|
|
};
|
2021-01-01 18:55:56 -06:00
|
|
|
}"#,
|
|
|
|
)
|
|
|
|
}
|
2021-03-04 07:43:52 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_pull_assignment_up_deref() {
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::check!(test_pull_assignment_up_deref);
|
2021-03-04 07:43:52 -06:00
|
|
|
check_assist(
|
|
|
|
pull_assignment_up,
|
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
let mut a = 1;
|
|
|
|
let b = &mut a;
|
|
|
|
|
|
|
|
if true {
|
|
|
|
$0*b = 2;
|
|
|
|
} else {
|
|
|
|
*b = 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn foo() {
|
|
|
|
let mut a = 1;
|
|
|
|
let b = &mut a;
|
|
|
|
|
|
|
|
*b = if true {
|
|
|
|
2
|
|
|
|
} else {
|
|
|
|
3
|
|
|
|
};
|
|
|
|
}
|
2021-05-08 12:40:07 -05:00
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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;
|
|
|
|
}
|
|
|
|
}
|
2021-03-04 07:43:52 -06:00
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
2021-01-01 18:55:56 -06:00
|
|
|
}
|