Skip to content

Commit c8a73fe

Browse files
Merge #6982
6982: Remove parentheses when inverting `!(cond)` r=matklad a=Jesse-Bakker Followup to #6894 When inverting a composite condition twice, the parentheses were left. This also removes those unnecessary parentheses when applying the invert-if assist. Co-authored-by: Jesse Bakker <[email protected]>
2 parents 71c8073 + 2e7abf8 commit c8a73fe

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)