Skip to content

Commit 8b45de5

Browse files
bors[bot]Veykril
andauthored
Merge #10411
10411: fix: Parenthesize expressions in `if_to_bool_then` assist where required r=Veykril a=Veykril bors r+ Co-authored-by: Lukas Wirth <[email protected]>
2 parents c30a6a2 + 816fafd commit 8b45de5

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

crates/ide_assists/src/handlers/convert_bool_then.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use crate::{
3939
// }
4040
// ```
4141
pub(crate) fn convert_if_to_bool_then(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
42-
// todo, applies to match as well
42+
// FIXME applies to match as well
4343
let expr = ctx.find_node_at_offset::<ast::IfExpr>()?;
4444
if !expr.if_token()?.text_range().contains_inclusive(ctx.offset()) {
4545
return None;
@@ -101,7 +101,29 @@ pub(crate) fn convert_if_to_bool_then(acc: &mut Assists, ctx: &AssistContext) ->
101101
e => e,
102102
};
103103

104+
let parenthesize = matches!(
105+
cond,
106+
ast::Expr::BinExpr(_)
107+
| ast::Expr::BlockExpr(_)
108+
| ast::Expr::BoxExpr(_)
109+
| ast::Expr::BreakExpr(_)
110+
| ast::Expr::CastExpr(_)
111+
| ast::Expr::ClosureExpr(_)
112+
| ast::Expr::ContinueExpr(_)
113+
| ast::Expr::ForExpr(_)
114+
| ast::Expr::IfExpr(_)
115+
| ast::Expr::LoopExpr(_)
116+
| ast::Expr::MacroCall(_)
117+
| ast::Expr::MatchExpr(_)
118+
| ast::Expr::PrefixExpr(_)
119+
| ast::Expr::RangeExpr(_)
120+
| ast::Expr::RefExpr(_)
121+
| ast::Expr::ReturnExpr(_)
122+
| ast::Expr::WhileExpr(_)
123+
| ast::Expr::YieldExpr(_)
124+
);
104125
let cond = if invert_cond { invert_boolean_expression(cond) } else { cond };
126+
let cond = if parenthesize { make::expr_paren(cond) } else { cond };
105127
let arg_list = make::arg_list(Some(make::expr_closure(None, closure_body)));
106128
let mcall = make::expr_method_call(cond, make::name_ref("then"), arg_list);
107129
builder.replace(target, mcall.to_string());

0 commit comments

Comments
 (0)