about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--crates/assists/src/handlers/invert_if.rs9
-rw-r--r--crates/assists/src/utils.rs4
-rw-r--r--crates/syntax/src/ast/make.rs3
3 files changed, 16 insertions, 0 deletions
diff --git a/crates/assists/src/handlers/invert_if.rs b/crates/assists/src/handlers/invert_if.rs
index ea722b91b2d..91e2f5c8cb3 100644
--- a/crates/assists/src/handlers/invert_if.rs
+++ b/crates/assists/src/handlers/invert_if.rs
@@ -69,6 +69,15 @@ mod tests {
     use crate::tests::{check_assist, check_assist_not_applicable};
 
     #[test]
+    fn invert_if_composite_condition() {
+        check_assist(
+            invert_if,
+            "fn f() { i<|>f x == 3 || x == 4 || x == 5 { 1 } else { 3 * 2 } }",
+            "fn f() { if !(x == 3 || x == 4 || x == 5) { 3 * 2 } else { 1 } }",
+        )
+    }
+
+    #[test]
     fn invert_if_remove_inequality() {
         check_assist(
             invert_if,
diff --git a/crates/assists/src/utils.rs b/crates/assists/src/utils.rs
index 01f5c291fb3..f2cacf7c80a 100644
--- a/crates/assists/src/utils.rs
+++ b/crates/assists/src/utils.rs
@@ -212,6 +212,10 @@ fn invert_special_case(expr: &ast::Expr) -> Option<ast::Expr> {
         ast::Expr::BinExpr(bin) => match bin.op_kind()? {
             ast::BinOp::NegatedEqualityTest => bin.replace_op(T![==]).map(|it| it.into()),
             ast::BinOp::EqualityTest => bin.replace_op(T![!=]).map(|it| it.into()),
+            // Parenthesize composite boolean expressions before prefixing `!`
+            ast::BinOp::BooleanAnd | ast::BinOp::BooleanOr => {
+                Some(make::expr_prefix(T![!], make::expr_paren(expr.clone())))
+            }
             _ => None,
         },
         ast::Expr::MethodCallExpr(mce) => {
diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs
index cc09b77a590..16b079c42ac 100644
--- a/crates/syntax/src/ast/make.rs
+++ b/crates/syntax/src/ast/make.rs
@@ -196,6 +196,9 @@ pub fn expr_method_call(receiver: ast::Expr, method: &str, arg_list: ast::ArgLis
 pub fn expr_ref(expr: ast::Expr, exclusive: bool) -> ast::Expr {
     expr_from_text(&if exclusive { format!("&mut {}", expr) } else { format!("&{}", expr) })
 }
+pub fn expr_paren(expr: ast::Expr) -> ast::Expr {
+    expr_from_text(&format!("({})", expr))
+}
 fn expr_from_text(text: &str) -> ast::Expr {
     ast_from_text(&format!("const C: () = {};", text))
 }