diff options
author | Aleksey Kladov <[email protected]> | 2019-11-13 07:27:21 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-11-13 07:32:53 +0000 |
commit | 018255efe3e456aa8d712f68a714d5c6e010d03f (patch) | |
tree | 8056453dcec060d3fcdea6cec5579e552975230c /crates/ra_assists | |
parent | 70dd70b1fcbbbe2e60849412412ef05e7d31eb0a (diff) |
Minor cleanup
Diffstat (limited to 'crates/ra_assists')
-rw-r--r-- | crates/ra_assists/src/assists/early_return.rs | 39 |
1 files changed, 20 insertions, 19 deletions
diff --git a/crates/ra_assists/src/assists/early_return.rs b/crates/ra_assists/src/assists/early_return.rs index 570a07a20..f44610001 100644 --- a/crates/ra_assists/src/assists/early_return.rs +++ b/crates/ra_assists/src/assists/early_return.rs | |||
@@ -38,27 +38,27 @@ use crate::{ | |||
38 | // ``` | 38 | // ``` |
39 | pub(crate) fn convert_to_guarded_return(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 39 | pub(crate) fn convert_to_guarded_return(ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { |
40 | let if_expr: ast::IfExpr = ctx.find_node_at_offset()?; | 40 | let if_expr: ast::IfExpr = ctx.find_node_at_offset()?; |
41 | if if_expr.else_branch().is_some() { | ||
42 | return None; | ||
43 | } | ||
44 | |||
41 | let cond = if_expr.condition()?; | 45 | let cond = if_expr.condition()?; |
42 | let mut if_let_ident: Option<String> = None; | ||
43 | 46 | ||
44 | // Check if there is an IfLet that we can handle. | 47 | // Check if there is an IfLet that we can handle. |
45 | match cond.pat() { | 48 | let if_let_ident = match cond.pat() { |
46 | None => {} // No IfLet, supported. | 49 | None => None, // No IfLet, supported. |
47 | Some(TupleStructPat(ref pat)) if pat.args().count() == 1usize => match &pat.path() { | 50 | Some(TupleStructPat(pat)) if pat.args().count() == 1 => { |
48 | Some(p) => match p.qualifier() { | 51 | let path = pat.path()?; |
49 | None => if_let_ident = Some(p.syntax().text().to_string()), | 52 | match path.qualifier() { |
50 | _ => return None, | 53 | None => Some(path.syntax().to_string()), |
51 | }, | 54 | Some(_) => return None, |
52 | _ => return None, | 55 | } |
53 | }, | 56 | } |
54 | _ => return None, // Unsupported IfLet. | 57 | Some(_) => return None, // Unsupported IfLet. |
55 | }; | 58 | }; |
56 | 59 | ||
57 | let expr = cond.expr()?; | 60 | let expr = cond.expr()?; |
58 | let then_block = if_expr.then_branch()?.block()?; | 61 | let then_block = if_expr.then_branch()?.block()?; |
59 | if if_expr.else_branch().is_some() { | ||
60 | return None; | ||
61 | } | ||
62 | 62 | ||
63 | let parent_block = if_expr.syntax().parent()?.ancestors().find_map(ast::Block::cast)?; | 63 | let parent_block = if_expr.syntax().parent()?.ancestors().find_map(ast::Block::cast)?; |
64 | 64 | ||
@@ -100,7 +100,7 @@ pub(crate) fn convert_to_guarded_return(ctx: AssistCtx<impl HirDatabase>) -> Opt | |||
100 | let early_expression = &(early_expression.to_owned() + ";"); | 100 | let early_expression = &(early_expression.to_owned() + ";"); |
101 | let new_expr = | 101 | let new_expr = |
102 | if_indent_level.increase_indent(make::if_expression(&expr, early_expression)); | 102 | if_indent_level.increase_indent(make::if_expression(&expr, early_expression)); |
103 | replace(new_expr, &then_block, &parent_block, &if_expr) | 103 | replace(new_expr.syntax(), &then_block, &parent_block, &if_expr) |
104 | } | 104 | } |
105 | Some(if_let_ident) => { | 105 | Some(if_let_ident) => { |
106 | // If-let. | 106 | // If-let. |
@@ -109,7 +109,7 @@ pub(crate) fn convert_to_guarded_return(ctx: AssistCtx<impl HirDatabase>) -> Opt | |||
109 | &if_let_ident, | 109 | &if_let_ident, |
110 | early_expression, | 110 | early_expression, |
111 | )); | 111 | )); |
112 | replace(new_expr, &then_block, &parent_block, &if_expr) | 112 | replace(new_expr.syntax(), &then_block, &parent_block, &if_expr) |
113 | } | 113 | } |
114 | }; | 114 | }; |
115 | edit.target(if_expr.syntax().text_range()); | 115 | edit.target(if_expr.syntax().text_range()); |
@@ -117,7 +117,7 @@ pub(crate) fn convert_to_guarded_return(ctx: AssistCtx<impl HirDatabase>) -> Opt | |||
117 | edit.set_cursor(cursor_position); | 117 | edit.set_cursor(cursor_position); |
118 | 118 | ||
119 | fn replace( | 119 | fn replace( |
120 | new_expr: impl AstNode, | 120 | new_expr: &SyntaxNode, |
121 | then_block: &Block, | 121 | then_block: &Block, |
122 | parent_block: &Block, | 122 | parent_block: &Block, |
123 | if_expr: &ast::IfExpr, | 123 | if_expr: &ast::IfExpr, |
@@ -130,7 +130,7 @@ pub(crate) fn convert_to_guarded_return(ctx: AssistCtx<impl HirDatabase>) -> Opt | |||
130 | } else { | 130 | } else { |
131 | end_of_then | 131 | end_of_then |
132 | }; | 132 | }; |
133 | let mut then_statements = new_expr.syntax().children_with_tokens().chain( | 133 | let mut then_statements = new_expr.children_with_tokens().chain( |
134 | then_block_items | 134 | then_block_items |
135 | .syntax() | 135 | .syntax() |
136 | .children_with_tokens() | 136 | .children_with_tokens() |
@@ -151,9 +151,10 @@ pub(crate) fn convert_to_guarded_return(ctx: AssistCtx<impl HirDatabase>) -> Opt | |||
151 | 151 | ||
152 | #[cfg(test)] | 152 | #[cfg(test)] |
153 | mod tests { | 153 | mod tests { |
154 | use super::*; | ||
155 | use crate::helpers::{check_assist, check_assist_not_applicable}; | 154 | use crate::helpers::{check_assist, check_assist_not_applicable}; |
156 | 155 | ||
156 | use super::*; | ||
157 | |||
157 | #[test] | 158 | #[test] |
158 | fn convert_inside_fn() { | 159 | fn convert_inside_fn() { |
159 | check_assist( | 160 | check_assist( |