Skip to content

Commit 2e7abf8

Browse files
committed
Remove parentheses when inverting !(cond)
1 parent 9bb9fba commit 2e7abf8

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

crates/assists/src/handlers/invert_if.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ mod tests {
7777
)
7878
}
7979

80+
#[test]
81+
fn invert_if_remove_not_parentheses() {
82+
check_assist(
83+
invert_if,
84+
"fn f() { i<|>f !(x == 3 || x == 4 || x == 5) { 3 * 2 } else { 1 } }",
85+
"fn f() { if x == 3 || x == 4 || x == 5 { 1 } else { 3 * 2 } }",
86+
)
87+
}
88+
8089
#[test]
8190
fn invert_if_remove_inequality() {
8291
check_assist(

crates/assists/src/utils.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,13 @@ fn invert_special_case(expr: &ast::Expr) -> Option<ast::Expr> {
232232
};
233233
Some(make::expr_method_call(receiver, method, arg_list))
234234
}
235-
ast::Expr::PrefixExpr(pe) if pe.op_kind()? == ast::PrefixOp::Not => pe.expr(),
235+
ast::Expr::PrefixExpr(pe) if pe.op_kind()? == ast::PrefixOp::Not => {
236+
if let ast::Expr::ParenExpr(parexpr) = pe.expr()? {
237+
parexpr.expr()
238+
} else {
239+
pe.expr()
240+
}
241+
}
236242
// FIXME:
237243
// ast::Expr::Literal(true | false )
238244
_ => None,

0 commit comments

Comments
 (0)