@@ -39,7 +39,7 @@ use crate::{
39
39
// }
40
40
// ```
41
41
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
43
43
let expr = ctx. find_node_at_offset :: < ast:: IfExpr > ( ) ?;
44
44
if !expr. if_token ( ) ?. text_range ( ) . contains_inclusive ( ctx. offset ( ) ) {
45
45
return None ;
@@ -101,7 +101,29 @@ pub(crate) fn convert_if_to_bool_then(acc: &mut Assists, ctx: &AssistContext) ->
101
101
e => e,
102
102
} ;
103
103
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
+ ) ;
104
125
let cond = if invert_cond { invert_boolean_expression ( cond) } else { cond } ;
126
+ let cond = if parenthesize { make:: expr_paren ( cond) } else { cond } ;
105
127
let arg_list = make:: arg_list ( Some ( make:: expr_closure ( None , closure_body) ) ) ;
106
128
let mcall = make:: expr_method_call ( cond, make:: name_ref ( "then" ) , arg_list) ;
107
129
builder. replace ( target, mcall. to_string ( ) ) ;
0 commit comments