mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 12:54:58 +00:00
De Morgan's Law assist now correctly parenthesizes binary expressions.
This commit is contained in:
parent
aa38fa1c72
commit
f7a4a87de2
3 changed files with 7 additions and 11 deletions
|
@ -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].
|
// 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 transforms expressions of the form `!l || !r` into `!(l && r)`.
|
||||||
// This also works with `&&`. This assist can only be applied with the cursor
|
// 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.
|
// on either `||` or `&&`.
|
||||||
// This means something of the form `!x` or `x != y`.
|
|
||||||
//
|
//
|
||||||
// ```
|
// ```
|
||||||
// fn main() {
|
// fn main() {
|
||||||
// if x != 4 ||$0 !y {}
|
// if x != 4 ||$0 y < 3 {}
|
||||||
// }
|
// }
|
||||||
// ```
|
// ```
|
||||||
// ->
|
// ->
|
||||||
// ```
|
// ```
|
||||||
// fn main() {
|
// fn main() {
|
||||||
// if !(x == 4 && y) {}
|
// if !(x == 4 && !(y < 3)) {}
|
||||||
// }
|
// }
|
||||||
// ```
|
// ```
|
||||||
pub(crate) fn apply_demorgan(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
pub(crate) fn apply_demorgan(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
||||||
|
|
|
@ -147,12 +147,12 @@ fn doctest_apply_demorgan() {
|
||||||
"apply_demorgan",
|
"apply_demorgan",
|
||||||
r#####"
|
r#####"
|
||||||
fn main() {
|
fn main() {
|
||||||
if x != 4 ||$0 !y {}
|
if x != 4 ||$0 y < 3 {}
|
||||||
}
|
}
|
||||||
"#####,
|
"#####,
|
||||||
r#####"
|
r#####"
|
||||||
fn main() {
|
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::Expr::BinExpr(bin) => match bin.op_kind()? {
|
||||||
ast::BinOp::NegatedEqualityTest => bin.replace_op(T![==]).map(|it| it.into()),
|
ast::BinOp::NegatedEqualityTest => bin.replace_op(T![==]).map(|it| it.into()),
|
||||||
ast::BinOp::EqualityTest => 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 `!`
|
// Parenthesize other expressions before prefixing `!`
|
||||||
ast::BinOp::BooleanAnd | ast::BinOp::BooleanOr => {
|
_ => Some(make::expr_prefix(T![!], make::expr_paren(expr.clone()))),
|
||||||
Some(make::expr_prefix(T![!], make::expr_paren(expr.clone())))
|
|
||||||
}
|
|
||||||
_ => None,
|
|
||||||
},
|
},
|
||||||
ast::Expr::MethodCallExpr(mce) => {
|
ast::Expr::MethodCallExpr(mce) => {
|
||||||
let receiver = mce.receiver()?;
|
let receiver = mce.receiver()?;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue