From 2e7abf83844ef0c7d807262a682941f2aef9d3d9 Mon Sep 17 00:00:00 2001 From: Jesse Bakker Date: Mon, 21 Dec 2020 17:37:38 +0100 Subject: Remove parentheses when inverting `!(cond)` --- crates/assists/src/handlers/invert_if.rs | 9 +++++++++ crates/assists/src/utils.rs | 8 +++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/crates/assists/src/handlers/invert_if.rs b/crates/assists/src/handlers/invert_if.rs index 91e2f5c8c..f9c33b3f7 100644 --- a/crates/assists/src/handlers/invert_if.rs +++ b/crates/assists/src/handlers/invert_if.rs @@ -77,6 +77,15 @@ mod tests { ) } + #[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( diff --git a/crates/assists/src/utils.rs b/crates/assists/src/utils.rs index f2cacf7c8..d41084b59 100644 --- a/crates/assists/src/utils.rs +++ b/crates/assists/src/utils.rs @@ -232,7 +232,13 @@ fn invert_special_case(expr: &ast::Expr) -> Option { }; 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, -- cgit v1.2.3