From 36ee9ecb678d775609bf3825f1c4fd8e0c56bf32 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 7 Feb 2020 11:51:16 +0100 Subject: Cleanup early return assist --- crates/ra_assists/src/assists/early_return.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'crates/ra_assists') diff --git a/crates/ra_assists/src/assists/early_return.rs b/crates/ra_assists/src/assists/early_return.rs index 3169be2b9..8f30dc586 100644 --- a/crates/ra_assists/src/assists/early_return.rs +++ b/crates/ra_assists/src/assists/early_return.rs @@ -10,6 +10,7 @@ use ra_syntax::{ use crate::{ assist_ctx::{Assist, AssistCtx}, + assists::invert_if::invert_boolean_expression, AssistId, }; @@ -99,9 +100,13 @@ pub(crate) fn convert_to_guarded_return(ctx: AssistCtx) -> Option { let new_block = match if_let_pat { None => { // If. - let early_expression = &(early_expression.syntax().to_string() + ";"); - let new_expr = if_indent_level - .increase_indent(make::if_expression(cond_expr, early_expression)); + let new_expr = { + let then_branch = + make::block_expr(once(make::expr_stmt(early_expression).into()), None); + let cond = invert_boolean_expression(cond_expr); + let e = make::expr_if(cond, then_branch); + if_indent_level.increase_indent(e) + }; replace(new_expr.syntax(), &then_block, &parent_block, &if_expr) } Some((path, bound_ident)) => { -- cgit v1.2.3