]> git.lizzy.rs Git - rust.git/commitdiff
Remove parentheses when inverting `!(cond)`
authorJesse Bakker <github@jessebakker.com>
Mon, 21 Dec 2020 16:37:38 +0000 (17:37 +0100)
committerJesse Bakker <github@jessebakker.com>
Mon, 21 Dec 2020 16:37:38 +0000 (17:37 +0100)
crates/assists/src/handlers/invert_if.rs
crates/assists/src/utils.rs

index 91e2f5c8cb33f93fe1088d48a0364214c18eb211..f9c33b3f7d1c23a8600a66ee95e71a82c8b9e37e 100644 (file)
@@ -77,6 +77,15 @@ fn invert_if_composite_condition() {
         )
     }
 
+    #[test]
+    fn invert_if_remove_not_parentheses() {
+        check_assist(
+            invert_if,
+            "fn f() { i<|>f !(x == 3 || x == 4 || x == 5) { 3 * 2 } else { 1 } }",
+            "fn f() { if x == 3 || x == 4 || x == 5 { 1 } else { 3 * 2 } }",
+        )
+    }
+
     #[test]
     fn invert_if_remove_inequality() {
         check_assist(
index f2cacf7c80a4fe83e0b8ab2fbef07e32ffa54c24..d41084b59944daad1b91e6827bd2c3119b7ef730 100644 (file)
@@ -232,7 +232,13 @@ fn invert_special_case(expr: &ast::Expr) -> Option<ast::Expr> {
             };
             Some(make::expr_method_call(receiver, method, arg_list))
         }
-        ast::Expr::PrefixExpr(pe) if pe.op_kind()? == ast::PrefixOp::Not => pe.expr(),
+        ast::Expr::PrefixExpr(pe) if pe.op_kind()? == ast::PrefixOp::Not => {
+            if let ast::Expr::ParenExpr(parexpr) = pe.expr()? {
+                parexpr.expr()
+            } else {
+                pe.expr()
+            }
+        }
         // FIXME:
         // ast::Expr::Literal(true | false )
         _ => None,