aboutsummaryrefslogtreecommitdiff
path: root/crates/assists/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/assists/src')
-rw-r--r--crates/assists/src/handlers/early_return.rs2
-rw-r--r--crates/assists/src/handlers/extract_struct_from_enum_variant.rs12
-rw-r--r--crates/assists/src/handlers/extract_variable.rs2
-rw-r--r--crates/assists/src/handlers/infer_function_return_type.rs4
-rw-r--r--crates/assists/src/handlers/inline_function.rs202
-rw-r--r--crates/assists/src/handlers/move_guard.rs4
-rw-r--r--crates/assists/src/handlers/pull_assignment_up.rs (renamed from crates/assists/src/handlers/extract_assignment.rs)151
-rw-r--r--crates/assists/src/handlers/replace_if_let_with_match.rs2
-rw-r--r--crates/assists/src/handlers/wrap_return_type_in_result.rs8
-rw-r--r--crates/assists/src/lib.rs10
-rw-r--r--crates/assists/src/tests/generated.rs81
-rw-r--r--crates/assists/src/utils.rs2
12 files changed, 393 insertions, 87 deletions
diff --git a/crates/assists/src/handlers/early_return.rs b/crates/assists/src/handlers/early_return.rs
index 7bcc318a9..2c48f32bf 100644
--- a/crates/assists/src/handlers/early_return.rs
+++ b/crates/assists/src/handlers/early_return.rs
@@ -69,7 +69,7 @@ pub(crate) fn convert_to_guarded_return(acc: &mut Assists, ctx: &AssistContext)
69 69
70 let parent_block = if_expr.syntax().parent()?.ancestors().find_map(ast::BlockExpr::cast)?; 70 let parent_block = if_expr.syntax().parent()?.ancestors().find_map(ast::BlockExpr::cast)?;
71 71
72 if parent_block.expr()? != if_expr.clone().into() { 72 if parent_block.tail_expr()? != if_expr.clone().into() {
73 return None; 73 return None;
74 } 74 }
75 75
diff --git a/crates/assists/src/handlers/extract_struct_from_enum_variant.rs b/crates/assists/src/handlers/extract_struct_from_enum_variant.rs
index 030b9cd0c..6f35a061c 100644
--- a/crates/assists/src/handlers/extract_struct_from_enum_variant.rs
+++ b/crates/assists/src/handlers/extract_struct_from_enum_variant.rs
@@ -117,10 +117,14 @@ fn existing_definition(db: &RootDatabase, variant_name: &ast::Name, variant: &Va
117 .into_iter() 117 .into_iter()
118 .filter(|(_, def)| match def { 118 .filter(|(_, def)| match def {
119 // only check type-namespace 119 // only check type-namespace
120 hir::ScopeDef::ModuleDef(def) => matches!(def, 120 hir::ScopeDef::ModuleDef(def) => matches!(
121 ModuleDef::Module(_) | ModuleDef::Adt(_) | 121 def,
122 ModuleDef::Variant(_) | ModuleDef::Trait(_) | 122 ModuleDef::Module(_)
123 ModuleDef::TypeAlias(_) | ModuleDef::BuiltinType(_) 123 | ModuleDef::Adt(_)
124 | ModuleDef::Variant(_)
125 | ModuleDef::Trait(_)
126 | ModuleDef::TypeAlias(_)
127 | ModuleDef::BuiltinType(_)
124 ), 128 ),
125 _ => false, 129 _ => false,
126 }) 130 })
diff --git a/crates/assists/src/handlers/extract_variable.rs b/crates/assists/src/handlers/extract_variable.rs
index 9957012fe..291809205 100644
--- a/crates/assists/src/handlers/extract_variable.rs
+++ b/crates/assists/src/handlers/extract_variable.rs
@@ -139,7 +139,7 @@ impl Anchor {
139 fn from(to_extract: &ast::Expr) -> Option<Anchor> { 139 fn from(to_extract: &ast::Expr) -> Option<Anchor> {
140 to_extract.syntax().ancestors().find_map(|node| { 140 to_extract.syntax().ancestors().find_map(|node| {
141 if let Some(expr) = 141 if let Some(expr) =
142 node.parent().and_then(ast::BlockExpr::cast).and_then(|it| it.expr()) 142 node.parent().and_then(ast::BlockExpr::cast).and_then(|it| it.tail_expr())
143 { 143 {
144 if expr.syntax() == &node { 144 if expr.syntax() == &node {
145 mark::hit!(test_extract_var_last_expr); 145 mark::hit!(test_extract_var_last_expr);
diff --git a/crates/assists/src/handlers/infer_function_return_type.rs b/crates/assists/src/handlers/infer_function_return_type.rs
index aa584eb03..f499cdfdc 100644
--- a/crates/assists/src/handlers/infer_function_return_type.rs
+++ b/crates/assists/src/handlers/infer_function_return_type.rs
@@ -89,7 +89,7 @@ fn extract_tail(ctx: &AssistContext) -> Option<(FnType, ast::Expr, InsertOrRepla
89 let body = closure.body()?; 89 let body = closure.body()?;
90 let body_start = body.syntax().first_token()?.text_range().start(); 90 let body_start = body.syntax().first_token()?.text_range().start();
91 let (tail_expr, wrap_expr) = match body { 91 let (tail_expr, wrap_expr) = match body {
92 ast::Expr::BlockExpr(block) => (block.expr()?, false), 92 ast::Expr::BlockExpr(block) => (block.tail_expr()?, false),
93 body => (body, true), 93 body => (body, true),
94 }; 94 };
95 95
@@ -101,7 +101,7 @@ fn extract_tail(ctx: &AssistContext) -> Option<(FnType, ast::Expr, InsertOrRepla
101 let action = ret_ty_to_action(func.ret_type(), rparen_pos)?; 101 let action = ret_ty_to_action(func.ret_type(), rparen_pos)?;
102 102
103 let body = func.body()?; 103 let body = func.body()?;
104 let tail_expr = body.expr()?; 104 let tail_expr = body.tail_expr()?;
105 105
106 let ret_range_end = body.l_curly_token()?.text_range().start(); 106 let ret_range_end = body.l_curly_token()?.text_range().start();
107 let ret_range = TextRange::new(rparen_pos, ret_range_end); 107 let ret_range = TextRange::new(rparen_pos, ret_range_end);
diff --git a/crates/assists/src/handlers/inline_function.rs b/crates/assists/src/handlers/inline_function.rs
new file mode 100644
index 000000000..6e351bdcd
--- /dev/null
+++ b/crates/assists/src/handlers/inline_function.rs
@@ -0,0 +1,202 @@
1use ast::make;
2use hir::{HasSource, PathResolution};
3use syntax::{
4 ast::{self, edit::AstNodeEdit, ArgListOwner},
5 AstNode,
6};
7use test_utils::mark;
8
9use crate::{
10 assist_context::{AssistContext, Assists},
11 AssistId, AssistKind,
12};
13
14// Assist: inline_function
15//
16// Inlines a function body.
17//
18// ```
19// fn add(a: u32, b: u32) -> u32 { a + b }
20// fn main() {
21// let x = add<|>(1, 2);
22// }
23// ```
24// ->
25// ```
26// fn add(a: u32, b: u32) -> u32 { a + b }
27// fn main() {
28// let x = {
29// let a = 1;
30// let b = 2;
31// a + b
32// };
33// }
34// ```
35pub(crate) fn inline_function(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
36 let path_expr: ast::PathExpr = ctx.find_node_at_offset()?;
37 let call = path_expr.syntax().parent().and_then(ast::CallExpr::cast)?;
38 let path = path_expr.path()?;
39
40 let function = match ctx.sema.resolve_path(&path)? {
41 PathResolution::Def(hir::ModuleDef::Function(f)) => f,
42 _ => return None,
43 };
44
45 let function_source = function.source(ctx.db())?;
46 let arguments: Vec<_> = call.arg_list()?.args().collect();
47 let parameters = function_parameter_patterns(&function_source.value)?;
48
49 if arguments.len() != parameters.len() {
50 // Can't inline the function because they've passed the wrong number of
51 // arguments to this function
52 mark::hit!(inline_function_incorrect_number_of_arguments);
53 return None;
54 }
55
56 let new_bindings = parameters.into_iter().zip(arguments);
57
58 let body = function_source.value.body()?;
59
60 acc.add(
61 AssistId("inline_function", AssistKind::RefactorInline),
62 format!("Inline `{}`", path),
63 call.syntax().text_range(),
64 |builder| {
65 let mut statements: Vec<ast::Stmt> = Vec::new();
66
67 for (pattern, value) in new_bindings {
68 statements.push(make::let_stmt(pattern, Some(value)).into());
69 }
70
71 statements.extend(body.statements());
72
73 let original_indentation = call.indent_level();
74 let replacement = make::block_expr(statements, body.tail_expr())
75 .reset_indent()
76 .indent(original_indentation);
77
78 builder.replace_ast(ast::Expr::CallExpr(call), ast::Expr::BlockExpr(replacement));
79 },
80 )
81}
82
83fn function_parameter_patterns(value: &ast::Fn) -> Option<Vec<ast::Pat>> {
84 let mut patterns = Vec::new();
85
86 for param in value.param_list()?.params() {
87 let pattern = param.pat()?;
88 patterns.push(pattern);
89 }
90
91 Some(patterns)
92}
93
94#[cfg(test)]
95mod tests {
96 use crate::tests::{check_assist, check_assist_not_applicable};
97
98 use super::*;
99
100 #[test]
101 fn no_args_or_return_value_gets_inlined_without_block() {
102 check_assist(
103 inline_function,
104 r#"
105fn foo() { println!("Hello, World!"); }
106fn main() {
107 fo<|>o();
108}
109"#,
110 r#"
111fn foo() { println!("Hello, World!"); }
112fn main() {
113 {
114 println!("Hello, World!");
115 };
116}
117"#,
118 );
119 }
120
121 #[test]
122 fn args_with_side_effects() {
123 check_assist(
124 inline_function,
125 r#"
126fn foo(name: String) { println!("Hello, {}!", name); }
127fn main() {
128 foo<|>(String::from("Michael"));
129}
130"#,
131 r#"
132fn foo(name: String) { println!("Hello, {}!", name); }
133fn main() {
134 {
135 let name = String::from("Michael");
136 println!("Hello, {}!", name);
137 };
138}
139"#,
140 );
141 }
142
143 #[test]
144 fn method_inlining_isnt_supported() {
145 check_assist_not_applicable(
146 inline_function,
147 r"
148struct Foo;
149impl Foo { fn bar(&self) {} }
150
151fn main() { Foo.bar<|>(); }
152",
153 );
154 }
155
156 #[test]
157 fn not_applicable_when_incorrect_number_of_parameters_are_provided() {
158 mark::check!(inline_function_incorrect_number_of_arguments);
159 check_assist_not_applicable(
160 inline_function,
161 r#"
162fn add(a: u32, b: u32) -> u32 { a + b }
163fn main() { let x = add<|>(42); }
164"#,
165 );
166 }
167
168 #[test]
169 fn function_with_multiple_statements() {
170 check_assist(
171 inline_function,
172 r#"
173fn foo(a: u32, b: u32) -> u32 {
174 let x = a + b;
175 let y = x - b;
176 x * y
177}
178
179fn main() {
180 let x = foo<|>(1, 2);
181}
182"#,
183 r#"
184fn foo(a: u32, b: u32) -> u32 {
185 let x = a + b;
186 let y = x - b;
187 x * y
188}
189
190fn main() {
191 let x = {
192 let a = 1;
193 let b = 2;
194 let x = a + b;
195 let y = x - b;
196 x * y
197 };
198}
199"#,
200 );
201 }
202}
diff --git a/crates/assists/src/handlers/move_guard.rs b/crates/assists/src/handlers/move_guard.rs
index eaffd80ce..4318ca6dc 100644
--- a/crates/assists/src/handlers/move_guard.rs
+++ b/crates/assists/src/handlers/move_guard.rs
@@ -98,7 +98,7 @@ pub(crate) fn move_arm_cond_to_match_guard(acc: &mut Assists, ctx: &AssistContex
98 let mut replace_node = None; 98 let mut replace_node = None;
99 let if_expr: IfExpr = IfExpr::cast(arm_body.syntax().clone()).or_else(|| { 99 let if_expr: IfExpr = IfExpr::cast(arm_body.syntax().clone()).or_else(|| {
100 let block_expr = BlockExpr::cast(arm_body.syntax().clone())?; 100 let block_expr = BlockExpr::cast(arm_body.syntax().clone())?;
101 if let Expr::IfExpr(e) = block_expr.expr()? { 101 if let Expr::IfExpr(e) = block_expr.tail_expr()? {
102 replace_node = Some(block_expr.syntax().clone()); 102 replace_node = Some(block_expr.syntax().clone());
103 Some(e) 103 Some(e)
104 } else { 104 } else {
@@ -128,7 +128,7 @@ pub(crate) fn move_arm_cond_to_match_guard(acc: &mut Assists, ctx: &AssistContex
128 |edit| { 128 |edit| {
129 let then_only_expr = then_block.statements().next().is_none(); 129 let then_only_expr = then_block.statements().next().is_none();
130 130
131 match &then_block.expr() { 131 match &then_block.tail_expr() {
132 Some(then_expr) if then_only_expr => { 132 Some(then_expr) if then_only_expr => {
133 edit.replace(replace_node.text_range(), then_expr.syntax().text()) 133 edit.replace(replace_node.text_range(), then_expr.syntax().text())
134 } 134 }
diff --git a/crates/assists/src/handlers/extract_assignment.rs b/crates/assists/src/handlers/pull_assignment_up.rs
index 281cf5d24..63b662fad 100644
--- a/crates/assists/src/handlers/extract_assignment.rs
+++ b/crates/assists/src/handlers/pull_assignment_up.rs
@@ -1,4 +1,3 @@
1use hir::AsName;
2use syntax::{ 1use syntax::{
3 ast::{self, edit::AstNodeEdit, make}, 2 ast::{self, edit::AstNodeEdit, make},
4 AstNode, 3 AstNode,
@@ -10,9 +9,9 @@ use crate::{
10 AssistId, AssistKind, 9 AssistId, AssistKind,
11}; 10};
12 11
13// Assist: extract_assignment 12// Assist: pull_assignment_up
14// 13//
15// Extracts variable assigment to outside an if or match statement. 14// Extracts variable assignment to outside an if or match statement.
16// 15//
17// ``` 16// ```
18// fn main() { 17// fn main() {
@@ -37,16 +36,24 @@ use crate::{
37// }; 36// };
38// } 37// }
39// ``` 38// ```
40pub(crate) fn extract_assigment(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { 39pub(crate) fn pull_assignment_up(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
41 let name = ctx.find_node_at_offset::<ast::NameRef>()?.as_name(); 40 let assign_expr = ctx.find_node_at_offset::<ast::BinExpr>()?;
41 let name_expr = if assign_expr.op_kind()? == ast::BinOp::Assignment {
42 assign_expr.lhs()?
43 } else {
44 return None;
45 };
42 46
43 let (old_stmt, new_stmt) = if let Some(if_expr) = ctx.find_node_at_offset::<ast::IfExpr>() { 47 let (old_stmt, new_stmt) = if let Some(if_expr) = ctx.find_node_at_offset::<ast::IfExpr>() {
44 ( 48 (
45 ast::Expr::cast(if_expr.syntax().to_owned())?, 49 ast::Expr::cast(if_expr.syntax().to_owned())?,
46 exprify_if(&if_expr, &name)?.indent(if_expr.indent_level()), 50 exprify_if(&if_expr, &ctx.sema, &name_expr)?.indent(if_expr.indent_level()),
47 ) 51 )
48 } else if let Some(match_expr) = ctx.find_node_at_offset::<ast::MatchExpr>() { 52 } else if let Some(match_expr) = ctx.find_node_at_offset::<ast::MatchExpr>() {
49 (ast::Expr::cast(match_expr.syntax().to_owned())?, exprify_match(&match_expr, &name)?) 53 (
54 ast::Expr::cast(match_expr.syntax().to_owned())?,
55 exprify_match(&match_expr, &ctx.sema, &name_expr)?,
56 )
50 } else { 57 } else {
51 return None; 58 return None;
52 }; 59 };
@@ -54,22 +61,26 @@ pub(crate) fn extract_assigment(acc: &mut Assists, ctx: &AssistContext) -> Optio
54 let expr_stmt = make::expr_stmt(new_stmt); 61 let expr_stmt = make::expr_stmt(new_stmt);
55 62
56 acc.add( 63 acc.add(
57 AssistId("extract_assignment", AssistKind::RefactorExtract), 64 AssistId("pull_assignment_up", AssistKind::RefactorExtract),
58 "Extract assignment", 65 "Pull assignment up",
59 old_stmt.syntax().text_range(), 66 old_stmt.syntax().text_range(),
60 move |edit| { 67 move |edit| {
61 edit.replace(old_stmt.syntax().text_range(), format!("{} = {};", name, expr_stmt)); 68 edit.replace(old_stmt.syntax().text_range(), format!("{} = {};", name_expr, expr_stmt));
62 }, 69 },
63 ) 70 )
64} 71}
65 72
66fn exprify_match(match_expr: &ast::MatchExpr, name: &hir::Name) -> Option<ast::Expr> { 73fn exprify_match(
74 match_expr: &ast::MatchExpr,
75 sema: &hir::Semantics<ide_db::RootDatabase>,
76 name: &ast::Expr,
77) -> Option<ast::Expr> {
67 let new_arm_list = match_expr 78 let new_arm_list = match_expr
68 .match_arm_list()? 79 .match_arm_list()?
69 .arms() 80 .arms()
70 .map(|arm| { 81 .map(|arm| {
71 if let ast::Expr::BlockExpr(block) = arm.expr()? { 82 if let ast::Expr::BlockExpr(block) = arm.expr()? {
72 let new_block = exprify_block(&block, name)?.indent(block.indent_level()); 83 let new_block = exprify_block(&block, sema, name)?.indent(block.indent_level());
73 Some(arm.replace_descendant(block, new_block)) 84 Some(arm.replace_descendant(block, new_block))
74 } else { 85 } else {
75 None 86 None
@@ -82,22 +93,32 @@ fn exprify_match(match_expr: &ast::MatchExpr, name: &hir::Name) -> Option<ast::E
82 Some(make::expr_match(match_expr.expr()?, new_arm_list)) 93 Some(make::expr_match(match_expr.expr()?, new_arm_list))
83} 94}
84 95
85fn exprify_if(statement: &ast::IfExpr, name: &hir::Name) -> Option<ast::Expr> { 96fn exprify_if(
86 let then_branch = exprify_block(&statement.then_branch()?, name)?; 97 statement: &ast::IfExpr,
98 sema: &hir::Semantics<ide_db::RootDatabase>,
99 name: &ast::Expr,
100) -> Option<ast::Expr> {
101 let then_branch = exprify_block(&statement.then_branch()?, sema, name)?;
87 let else_branch = match statement.else_branch()? { 102 let else_branch = match statement.else_branch()? {
88 ast::ElseBranch::Block(ref block) => ast::ElseBranch::Block(exprify_block(block, name)?), 103 ast::ElseBranch::Block(ref block) => {
104 ast::ElseBranch::Block(exprify_block(block, sema, name)?)
105 }
89 ast::ElseBranch::IfExpr(expr) => { 106 ast::ElseBranch::IfExpr(expr) => {
90 mark::hit!(test_extract_assigment_chained_if); 107 mark::hit!(test_pull_assignment_up_chained_if);
91 ast::ElseBranch::IfExpr(ast::IfExpr::cast( 108 ast::ElseBranch::IfExpr(ast::IfExpr::cast(
92 exprify_if(&expr, name)?.syntax().to_owned(), 109 exprify_if(&expr, sema, name)?.syntax().to_owned(),
93 )?) 110 )?)
94 } 111 }
95 }; 112 };
96 Some(make::expr_if(statement.condition()?, then_branch, Some(else_branch))) 113 Some(make::expr_if(statement.condition()?, then_branch, Some(else_branch)))
97} 114}
98 115
99fn exprify_block(block: &ast::BlockExpr, name: &hir::Name) -> Option<ast::BlockExpr> { 116fn exprify_block(
100 if block.expr().is_some() { 117 block: &ast::BlockExpr,
118 sema: &hir::Semantics<ide_db::RootDatabase>,
119 name: &ast::Expr,
120) -> Option<ast::BlockExpr> {
121 if block.tail_expr().is_some() {
101 return None; 122 return None;
102 } 123 }
103 124
@@ -106,8 +127,7 @@ fn exprify_block(block: &ast::BlockExpr, name: &hir::Name) -> Option<ast::BlockE
106 127
107 if let ast::Stmt::ExprStmt(stmt) = stmt { 128 if let ast::Stmt::ExprStmt(stmt) = stmt {
108 if let ast::Expr::BinExpr(expr) = stmt.expr()? { 129 if let ast::Expr::BinExpr(expr) = stmt.expr()? {
109 if expr.op_kind()? == ast::BinOp::Assignment 130 if expr.op_kind()? == ast::BinOp::Assignment && is_equivalent(sema, &expr.lhs()?, name)
110 && &expr.lhs()?.name_ref()?.as_name() == name
111 { 131 {
112 // The last statement in the block is an assignment to the name we want 132 // The last statement in the block is an assignment to the name we want
113 return Some(make::block_expr(stmts, Some(expr.rhs()?))); 133 return Some(make::block_expr(stmts, Some(expr.rhs()?)));
@@ -117,6 +137,29 @@ fn exprify_block(block: &ast::BlockExpr, name: &hir::Name) -> Option<ast::BlockE
117 None 137 None
118} 138}
119 139
140fn is_equivalent(
141 sema: &hir::Semantics<ide_db::RootDatabase>,
142 expr0: &ast::Expr,
143 expr1: &ast::Expr,
144) -> bool {
145 match (expr0, expr1) {
146 (ast::Expr::FieldExpr(field_expr0), ast::Expr::FieldExpr(field_expr1)) => {
147 mark::hit!(test_pull_assignment_up_field_assignment);
148 sema.resolve_field(field_expr0) == sema.resolve_field(field_expr1)
149 }
150 (ast::Expr::PathExpr(path0), ast::Expr::PathExpr(path1)) => {
151 let path0 = path0.path();
152 let path1 = path1.path();
153 if let (Some(path0), Some(path1)) = (path0, path1) {
154 sema.resolve_path(&path0) == sema.resolve_path(&path1)
155 } else {
156 false
157 }
158 }
159 _ => false,
160 }
161}
162
120#[cfg(test)] 163#[cfg(test)]
121mod tests { 164mod tests {
122 use super::*; 165 use super::*;
@@ -124,9 +167,9 @@ mod tests {
124 use crate::tests::{check_assist, check_assist_not_applicable}; 167 use crate::tests::{check_assist, check_assist_not_applicable};
125 168
126 #[test] 169 #[test]
127 fn test_extract_assignment_if() { 170 fn test_pull_assignment_up_if() {
128 check_assist( 171 check_assist(
129 extract_assigment, 172 pull_assignment_up,
130 r#" 173 r#"
131fn foo() { 174fn foo() {
132 let mut a = 1; 175 let mut a = 1;
@@ -151,9 +194,9 @@ fn foo() {
151 } 194 }
152 195
153 #[test] 196 #[test]
154 fn test_extract_assignment_match() { 197 fn test_pull_assignment_up_match() {
155 check_assist( 198 check_assist(
156 extract_assigment, 199 pull_assignment_up,
157 r#" 200 r#"
158fn foo() { 201fn foo() {
159 let mut a = 1; 202 let mut a = 1;
@@ -190,9 +233,9 @@ fn foo() {
190 } 233 }
191 234
192 #[test] 235 #[test]
193 fn test_extract_assignment_not_last_not_applicable() { 236 fn test_pull_assignment_up_not_last_not_applicable() {
194 check_assist_not_applicable( 237 check_assist_not_applicable(
195 extract_assigment, 238 pull_assignment_up,
196 r#" 239 r#"
197fn foo() { 240fn foo() {
198 let mut a = 1; 241 let mut a = 1;
@@ -208,10 +251,10 @@ fn foo() {
208 } 251 }
209 252
210 #[test] 253 #[test]
211 fn test_extract_assignment_chained_if() { 254 fn test_pull_assignment_up_chained_if() {
212 mark::check!(test_extract_assigment_chained_if); 255 mark::check!(test_pull_assignment_up_chained_if);
213 check_assist( 256 check_assist(
214 extract_assigment, 257 pull_assignment_up,
215 r#" 258 r#"
216fn foo() { 259fn foo() {
217 let mut a = 1; 260 let mut a = 1;
@@ -240,9 +283,9 @@ fn foo() {
240 } 283 }
241 284
242 #[test] 285 #[test]
243 fn test_extract_assigment_retains_stmts() { 286 fn test_pull_assignment_up_retains_stmts() {
244 check_assist( 287 check_assist(
245 extract_assigment, 288 pull_assignment_up,
246 r#" 289 r#"
247fn foo() { 290fn foo() {
248 let mut a = 1; 291 let mut a = 1;
@@ -271,9 +314,9 @@ fn foo() {
271 } 314 }
272 315
273 #[test] 316 #[test]
274 fn extract_assignment_let_stmt_not_applicable() { 317 fn pull_assignment_up_let_stmt_not_applicable() {
275 check_assist_not_applicable( 318 check_assist_not_applicable(
276 extract_assigment, 319 pull_assignment_up,
277 r#" 320 r#"
278fn foo() { 321fn foo() {
279 let mut a = 1; 322 let mut a = 1;
@@ -288,9 +331,9 @@ fn foo() {
288 } 331 }
289 332
290 #[test] 333 #[test]
291 fn extract_assignment_if_missing_assigment_not_applicable() { 334 fn pull_assignment_up_if_missing_assigment_not_applicable() {
292 check_assist_not_applicable( 335 check_assist_not_applicable(
293 extract_assigment, 336 pull_assignment_up,
294 r#" 337 r#"
295fn foo() { 338fn foo() {
296 let mut a = 1; 339 let mut a = 1;
@@ -303,9 +346,9 @@ fn foo() {
303 } 346 }
304 347
305 #[test] 348 #[test]
306 fn extract_assignment_match_missing_assigment_not_applicable() { 349 fn pull_assignment_up_match_missing_assigment_not_applicable() {
307 check_assist_not_applicable( 350 check_assist_not_applicable(
308 extract_assigment, 351 pull_assignment_up,
309 r#" 352 r#"
310fn foo() { 353fn foo() {
311 let mut a = 1; 354 let mut a = 1;
@@ -322,4 +365,36 @@ fn foo() {
322}"#, 365}"#,
323 ) 366 )
324 } 367 }
368
369 #[test]
370 fn test_pull_assignment_up_field_assignment() {
371 mark::check!(test_pull_assignment_up_field_assignment);
372 check_assist(
373 pull_assignment_up,
374 r#"
375struct A(usize);
376
377fn foo() {
378 let mut a = A(1);
379
380 if true {
381 <|>a.0 = 2;
382 } else {
383 a.0 = 3;
384 }
385}"#,
386 r#"
387struct A(usize);
388
389fn foo() {
390 let mut a = A(1);
391
392 a.0 = if true {
393 2
394 } else {
395 3
396 };
397}"#,
398 )
399 }
325} 400}
diff --git a/crates/assists/src/handlers/replace_if_let_with_match.rs b/crates/assists/src/handlers/replace_if_let_with_match.rs
index 4a355c66f..b67219222 100644
--- a/crates/assists/src/handlers/replace_if_let_with_match.rs
+++ b/crates/assists/src/handlers/replace_if_let_with_match.rs
@@ -138,7 +138,7 @@ pub(crate) fn replace_match_with_if_let(acc: &mut Assists, ctx: &AssistContext)
138 }; 138 };
139 let else_expr = match else_expr { 139 let else_expr = match else_expr {
140 ast::Expr::BlockExpr(block) 140 ast::Expr::BlockExpr(block)
141 if block.statements().count() == 0 && block.expr().is_none() => 141 if block.statements().count() == 0 && block.tail_expr().is_none() =>
142 { 142 {
143 None 143 None
144 } 144 }
diff --git a/crates/assists/src/handlers/wrap_return_type_in_result.rs b/crates/assists/src/handlers/wrap_return_type_in_result.rs
index 59e5debb1..358b61046 100644
--- a/crates/assists/src/handlers/wrap_return_type_in_result.rs
+++ b/crates/assists/src/handlers/wrap_return_type_in_result.rs
@@ -98,7 +98,7 @@ impl TailReturnCollector {
98 } 98 }
99 99
100 // Browse tail expressions for each block 100 // Browse tail expressions for each block
101 if let Some(expr) = block_expr.expr() { 101 if let Some(expr) = block_expr.tail_expr() {
102 if let Some(last_exprs) = get_tail_expr_from_block(&expr) { 102 if let Some(last_exprs) = get_tail_expr_from_block(&expr) {
103 for last_expr in last_exprs { 103 for last_expr in last_exprs {
104 let last_expr = match last_expr { 104 let last_expr = match last_expr {
@@ -170,7 +170,7 @@ impl TailReturnCollector {
170 } 170 }
171 171
172 fn collect_tail_exprs(&mut self, block: &BlockExpr) { 172 fn collect_tail_exprs(&mut self, block: &BlockExpr) {
173 if let Some(expr) = block.expr() { 173 if let Some(expr) = block.tail_expr() {
174 self.handle_exprs(&expr, true); 174 self.handle_exprs(&expr, true);
175 self.fetch_tail_exprs(&expr); 175 self.fetch_tail_exprs(&expr);
176 } 176 }
@@ -206,7 +206,7 @@ fn get_tail_expr_from_block(expr: &Expr) -> Option<Vec<NodeType>> {
206 Expr::IfExpr(if_expr) => { 206 Expr::IfExpr(if_expr) => {
207 let mut nodes = vec![]; 207 let mut nodes = vec![];
208 for block in if_expr.blocks() { 208 for block in if_expr.blocks() {
209 if let Some(block_expr) = block.expr() { 209 if let Some(block_expr) = block.tail_expr() {
210 if let Some(tail_exprs) = get_tail_expr_from_block(&block_expr) { 210 if let Some(tail_exprs) = get_tail_expr_from_block(&block_expr) {
211 nodes.extend(tail_exprs); 211 nodes.extend(tail_exprs);
212 } 212 }
@@ -228,7 +228,7 @@ fn get_tail_expr_from_block(expr: &Expr) -> Option<Vec<NodeType>> {
228 while_expr.syntax().last_child().map(|lc| vec![NodeType::Node(lc)]) 228 while_expr.syntax().last_child().map(|lc| vec![NodeType::Node(lc)])
229 } 229 }
230 Expr::BlockExpr(block_expr) => { 230 Expr::BlockExpr(block_expr) => {
231 block_expr.expr().map(|lc| vec![NodeType::Node(lc.syntax().clone())]) 231 block_expr.tail_expr().map(|lc| vec![NodeType::Node(lc.syntax().clone())])
232 } 232 }
233 Expr::MatchExpr(match_expr) => { 233 Expr::MatchExpr(match_expr) => {
234 let arm_list = match_expr.match_arm_list()?; 234 let arm_list = match_expr.match_arm_list()?;
diff --git a/crates/assists/src/lib.rs b/crates/assists/src/lib.rs
index 212464f85..9c2a95735 100644
--- a/crates/assists/src/lib.rs
+++ b/crates/assists/src/lib.rs
@@ -116,7 +116,6 @@ mod handlers {
116 mod convert_integer_literal; 116 mod convert_integer_literal;
117 mod early_return; 117 mod early_return;
118 mod expand_glob_import; 118 mod expand_glob_import;
119 mod extract_assignment;
120 mod extract_module_to_file; 119 mod extract_module_to_file;
121 mod extract_struct_from_enum_variant; 120 mod extract_struct_from_enum_variant;
122 mod extract_variable; 121 mod extract_variable;
@@ -125,13 +124,14 @@ mod handlers {
125 mod flip_binexpr; 124 mod flip_binexpr;
126 mod flip_comma; 125 mod flip_comma;
127 mod flip_trait_bound; 126 mod flip_trait_bound;
128 mod generate_derive;
129 mod generate_default_from_enum_variant; 127 mod generate_default_from_enum_variant;
128 mod generate_derive;
130 mod generate_from_impl_for_enum; 129 mod generate_from_impl_for_enum;
131 mod generate_function; 130 mod generate_function;
132 mod generate_impl; 131 mod generate_impl;
133 mod generate_new; 132 mod generate_new;
134 mod infer_function_return_type; 133 mod infer_function_return_type;
134 mod inline_function;
135 mod inline_local_variable; 135 mod inline_local_variable;
136 mod introduce_named_lifetime; 136 mod introduce_named_lifetime;
137 mod invert_if; 137 mod invert_if;
@@ -139,6 +139,7 @@ mod handlers {
139 mod merge_match_arms; 139 mod merge_match_arms;
140 mod move_bounds; 140 mod move_bounds;
141 mod move_guard; 141 mod move_guard;
142 mod pull_assignment_up;
142 mod qualify_path; 143 mod qualify_path;
143 mod raw_string; 144 mod raw_string;
144 mod remove_dbg; 145 mod remove_dbg;
@@ -168,7 +169,6 @@ mod handlers {
168 convert_integer_literal::convert_integer_literal, 169 convert_integer_literal::convert_integer_literal,
169 early_return::convert_to_guarded_return, 170 early_return::convert_to_guarded_return,
170 expand_glob_import::expand_glob_import, 171 expand_glob_import::expand_glob_import,
171 extract_assignment::extract_assigment,
172 extract_module_to_file::extract_module_to_file, 172 extract_module_to_file::extract_module_to_file,
173 extract_struct_from_enum_variant::extract_struct_from_enum_variant, 173 extract_struct_from_enum_variant::extract_struct_from_enum_variant,
174 extract_variable::extract_variable, 174 extract_variable::extract_variable,
@@ -177,13 +177,14 @@ mod handlers {
177 flip_binexpr::flip_binexpr, 177 flip_binexpr::flip_binexpr,
178 flip_comma::flip_comma, 178 flip_comma::flip_comma,
179 flip_trait_bound::flip_trait_bound, 179 flip_trait_bound::flip_trait_bound,
180 generate_derive::generate_derive,
181 generate_default_from_enum_variant::generate_default_from_enum_variant, 180 generate_default_from_enum_variant::generate_default_from_enum_variant,
181 generate_derive::generate_derive,
182 generate_from_impl_for_enum::generate_from_impl_for_enum, 182 generate_from_impl_for_enum::generate_from_impl_for_enum,
183 generate_function::generate_function, 183 generate_function::generate_function,
184 generate_impl::generate_impl, 184 generate_impl::generate_impl,
185 generate_new::generate_new, 185 generate_new::generate_new,
186 infer_function_return_type::infer_function_return_type, 186 infer_function_return_type::infer_function_return_type,
187 inline_function::inline_function,
187 inline_local_variable::inline_local_variable, 188 inline_local_variable::inline_local_variable,
188 introduce_named_lifetime::introduce_named_lifetime, 189 introduce_named_lifetime::introduce_named_lifetime,
189 invert_if::invert_if, 190 invert_if::invert_if,
@@ -192,6 +193,7 @@ mod handlers {
192 move_bounds::move_bounds_to_where_clause, 193 move_bounds::move_bounds_to_where_clause,
193 move_guard::move_arm_cond_to_match_guard, 194 move_guard::move_arm_cond_to_match_guard,
194 move_guard::move_guard_to_arm_body, 195 move_guard::move_guard_to_arm_body,
196 pull_assignment_up::pull_assignment_up,
195 qualify_path::qualify_path, 197 qualify_path::qualify_path,
196 raw_string::add_hash, 198 raw_string::add_hash,
197 raw_string::make_usual_string, 199 raw_string::make_usual_string,
diff --git a/crates/assists/src/tests/generated.rs b/crates/assists/src/tests/generated.rs
index b91a816e8..b15352cf3 100644
--- a/crates/assists/src/tests/generated.rs
+++ b/crates/assists/src/tests/generated.rs
@@ -238,35 +238,6 @@ fn qux(bar: Bar, baz: Baz) {}
238} 238}
239 239
240#[test] 240#[test]
241fn doctest_extract_assignment() {
242 check_doc_test(
243 "extract_assignment",
244 r#####"
245fn main() {
246 let mut foo = 6;
247
248 if true {
249 <|>foo = 5;
250 } else {
251 foo = 4;
252 }
253}
254"#####,
255 r#####"
256fn main() {
257 let mut foo = 6;
258
259 foo = if true {
260 5
261 } else {
262 4
263 };
264}
265"#####,
266 )
267}
268
269#[test]
270fn doctest_extract_module_to_file() { 241fn doctest_extract_module_to_file() {
271 check_doc_test( 242 check_doc_test(
272 "extract_module_to_file", 243 "extract_module_to_file",
@@ -560,6 +531,29 @@ fn foo() -> i32 { 42i32 }
560} 531}
561 532
562#[test] 533#[test]
534fn doctest_inline_function() {
535 check_doc_test(
536 "inline_function",
537 r#####"
538fn add(a: u32, b: u32) -> u32 { a + b }
539fn main() {
540 let x = add<|>(1, 2);
541}
542"#####,
543 r#####"
544fn add(a: u32, b: u32) -> u32 { a + b }
545fn main() {
546 let x = {
547 let a = 1;
548 let b = 2;
549 a + b
550 };
551}
552"#####,
553 )
554}
555
556#[test]
563fn doctest_inline_local_variable() { 557fn doctest_inline_local_variable() {
564 check_doc_test( 558 check_doc_test(
565 "inline_local_variable", 559 "inline_local_variable",
@@ -767,6 +761,35 @@ fn handle(action: Action) {
767} 761}
768 762
769#[test] 763#[test]
764fn doctest_pull_assignment_up() {
765 check_doc_test(
766 "pull_assignment_up",
767 r#####"
768fn main() {
769 let mut foo = 6;
770
771 if true {
772 <|>foo = 5;
773 } else {
774 foo = 4;
775 }
776}
777"#####,
778 r#####"
779fn main() {
780 let mut foo = 6;
781
782 foo = if true {
783 5
784 } else {
785 4
786 };
787}
788"#####,
789 )
790}
791
792#[test]
770fn doctest_qualify_path() { 793fn doctest_qualify_path() {
771 check_doc_test( 794 check_doc_test(
772 "qualify_path", 795 "qualify_path",
diff --git a/crates/assists/src/utils.rs b/crates/assists/src/utils.rs
index b05596446..8212cd129 100644
--- a/crates/assists/src/utils.rs
+++ b/crates/assists/src/utils.rs
@@ -37,7 +37,7 @@ pub fn extract_trivial_expression(block: &ast::BlockExpr) -> Option<ast::Expr> {
37 non_trivial_children.next().is_some() 37 non_trivial_children.next().is_some()
38 }; 38 };
39 39
40 if let Some(expr) = block.expr() { 40 if let Some(expr) = block.tail_expr() {
41 if has_anything_else(expr.syntax()) { 41 if has_anything_else(expr.syntax()) {
42 return None; 42 return None;
43 } 43 }