De Morgan's Law assist now correctly parenthesizes binary expressions.
This commit is contained in:
parent
aa38fa1c72
commit
f7a4a87de2
@ -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<()> {
|
||||
|
@ -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)) {}
|
||||
}
|
||||
"#####,
|
||||
)
|
||||
|
@ -217,11 +217,8 @@ 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,
|
||||
// Parenthesize other expressions before prefixing `!`
|
||||
_ => Some(make::expr_prefix(T![!], make::expr_paren(expr.clone()))),
|
||||
},
|
||||
ast::Expr::MethodCallExpr(mce) => {
|
||||
let receiver = mce.receiver()?;
|
||||
|
Loading…
x
Reference in New Issue
Block a user