diff options
| author | Lukas Wirth <lukastw97@gmail.com> | 2021-10-01 12:19:01 +0200 |
|---|---|---|
| committer | Lukas Wirth <lukastw97@gmail.com> | 2021-10-01 12:19:01 +0200 |
| commit | 816fafd99733c4f11bac05a17ad5416dee49db8c (patch) | |
| tree | 98da0f81e4ff93bd39a8216fbdbad967e0f85efe | |
| parent | 529b7a4167aab91257da02277c00e5cba245babb (diff) | |
| download | rust-816fafd99733c4f11bac05a17ad5416dee49db8c.tar.gz rust-816fafd99733c4f11bac05a17ad5416dee49db8c.zip | |
Parenthesize expressions in if_to_bool_then assist where required
| -rw-r--r-- | crates/ide_assists/src/handlers/convert_bool_then.rs | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/crates/ide_assists/src/handlers/convert_bool_then.rs b/crates/ide_assists/src/handlers/convert_bool_then.rs index 2e24c22c9fc..b8c55eb852f 100644 --- a/crates/ide_assists/src/handlers/convert_bool_then.rs +++ b/crates/ide_assists/src/handlers/convert_bool_then.rs @@ -39,7 +39,7 @@ use crate::{ // } // ``` pub(crate) fn convert_if_to_bool_then(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { - // todo, applies to match as well + // FIXME applies to match as well let expr = ctx.find_node_at_offset::<ast::IfExpr>()?; if !expr.if_token()?.text_range().contains_inclusive(ctx.offset()) { return None; @@ -101,7 +101,29 @@ pub(crate) fn convert_if_to_bool_then(acc: &mut Assists, ctx: &AssistContext) -> e => e, }; + let parenthesize = matches!( + cond, + ast::Expr::BinExpr(_) + | ast::Expr::BlockExpr(_) + | ast::Expr::BoxExpr(_) + | ast::Expr::BreakExpr(_) + | ast::Expr::CastExpr(_) + | ast::Expr::ClosureExpr(_) + | ast::Expr::ContinueExpr(_) + | ast::Expr::ForExpr(_) + | ast::Expr::IfExpr(_) + | ast::Expr::LoopExpr(_) + | ast::Expr::MacroCall(_) + | ast::Expr::MatchExpr(_) + | ast::Expr::PrefixExpr(_) + | ast::Expr::RangeExpr(_) + | ast::Expr::RefExpr(_) + | ast::Expr::ReturnExpr(_) + | ast::Expr::WhileExpr(_) + | ast::Expr::YieldExpr(_) + ); let cond = if invert_cond { invert_boolean_expression(cond) } else { cond }; + let cond = if parenthesize { make::expr_paren(cond) } else { cond }; let arg_list = make::arg_list(Some(make::expr_closure(None, closure_body))); let mcall = make::expr_method_call(cond, make::name_ref("then"), arg_list); builder.replace(target, mcall.to_string()); |
