From f7a4a87de2840789e12722afc7df9f4db2db013c Mon Sep 17 00:00:00 2001 From: lbrande Date: Fri, 19 Feb 2021 14:48:07 +0100 Subject: De Morgan's Law assist now correctly parenthesizes binary expressions. --- crates/ide_assists/src/handlers/apply_demorgan.rs | 7 +++---- crates/ide_assists/src/tests/generated.rs | 4 ++-- crates/ide_assists/src/utils.rs | 7 ++----- 3 files changed, 7 insertions(+), 11 deletions(-) diff --git a/crates/ide_assists/src/handlers/apply_demorgan.rs b/crates/ide_assists/src/handlers/apply_demorgan.rs index ed4d11455..e478ff2ce 100644 --- a/crates/ide_assists/src/handlers/apply_demorgan.rs +++ b/crates/ide_assists/src/handlers/apply_demorgan.rs @@ -7,18 +7,17 @@ use crate::{utils::invert_boolean_expression, AssistContext, AssistId, AssistKin // Apply https://en.wikipedia.org/wiki/De_Morgan%27s_laws[De Morgan's law]. // This transforms expressions of the form `!l || !r` into `!(l && r)`. // This also works with `&&`. This assist can only be applied with the cursor -// on either `||` or `&&`, with both operands being a negation of some kind. -// This means something of the form `!x` or `x != y`. +// on either `||` or `&&`. // // ``` // fn main() { -// if x != 4 ||$0 !y {} +// if x != 4 ||$0 y < 3 {} // } // ``` // -> // ``` // fn main() { -// if !(x == 4 && y) {} +// if !(x == 4 && !(y < 3)) {} // } // ``` pub(crate) fn apply_demorgan(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { diff --git a/crates/ide_assists/src/tests/generated.rs b/crates/ide_assists/src/tests/generated.rs index 0516deaff..ee9c870e8 100644 --- a/crates/ide_assists/src/tests/generated.rs +++ b/crates/ide_assists/src/tests/generated.rs @@ -147,12 +147,12 @@ fn doctest_apply_demorgan() { "apply_demorgan", r#####" fn main() { - if x != 4 ||$0 !y {} + if x != 4 ||$0 y < 3 {} } "#####, r#####" fn main() { - if !(x == 4 && y) {} + if !(x == 4 && !(y < 3)) {} } "#####, ) diff --git a/crates/ide_assists/src/utils.rs b/crates/ide_assists/src/utils.rs index 0074da741..cd026d432 100644 --- a/crates/ide_assists/src/utils.rs +++ b/crates/ide_assists/src/utils.rs @@ -217,11 +217,8 @@ fn invert_special_case(expr: &ast::Expr) -> Option { 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, + // Parenthesize other expressions before prefixing `!` + _ => Some(make::expr_prefix(T![!], make::expr_paren(expr.clone()))), }, ast::Expr::MethodCallExpr(mce) => { let receiver = mce.receiver()?; -- cgit v1.2.3