aboutsummaryrefslogtreecommitdiff
path: root/crates/assists
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-12-16 08:20:11 +0000
committerGitHub <[email protected]>2020-12-16 08:20:11 +0000
commitece626fe81579b6b38cdbd17d3e47bb422360a56 (patch)
tree3d7936e0c994fbf931085fd7e4dc1eb203d2cb54 /crates/assists
parenteb9ba457b0126bd45c45ef751e92ccfdc5fdc206 (diff)
parent0f42a71806ad62cd042dd40ab42501180fb72999 (diff)
Merge #6894
6894: Parenthesize composite if condition before inverting in invert-if assist r=matklad a=Jesse-Bakker Fixes #6867 Co-authored-by: Jesse Bakker <[email protected]>
Diffstat (limited to 'crates/assists')
-rw-r--r--crates/assists/src/handlers/invert_if.rs9
-rw-r--r--crates/assists/src/utils.rs4
2 files changed, 13 insertions, 0 deletions
diff --git a/crates/assists/src/handlers/invert_if.rs b/crates/assists/src/handlers/invert_if.rs
index ea722b91b..91e2f5c8c 100644
--- a/crates/assists/src/handlers/invert_if.rs
+++ b/crates/assists/src/handlers/invert_if.rs
@@ -69,6 +69,15 @@ mod tests {
69 use crate::tests::{check_assist, check_assist_not_applicable}; 69 use crate::tests::{check_assist, check_assist_not_applicable};
70 70
71 #[test] 71 #[test]
72 fn invert_if_composite_condition() {
73 check_assist(
74 invert_if,
75 "fn f() { i<|>f x == 3 || x == 4 || x == 5 { 1 } else { 3 * 2 } }",
76 "fn f() { if !(x == 3 || x == 4 || x == 5) { 3 * 2 } else { 1 } }",
77 )
78 }
79
80 #[test]
72 fn invert_if_remove_inequality() { 81 fn invert_if_remove_inequality() {
73 check_assist( 82 check_assist(
74 invert_if, 83 invert_if,
diff --git a/crates/assists/src/utils.rs b/crates/assists/src/utils.rs
index 01f5c291f..f2cacf7c8 100644
--- a/crates/assists/src/utils.rs
+++ b/crates/assists/src/utils.rs
@@ -212,6 +212,10 @@ fn invert_special_case(expr: &ast::Expr) -> Option<ast::Expr> {
212 ast::Expr::BinExpr(bin) => match bin.op_kind()? { 212 ast::Expr::BinExpr(bin) => match bin.op_kind()? {
213 ast::BinOp::NegatedEqualityTest => bin.replace_op(T![==]).map(|it| it.into()), 213 ast::BinOp::NegatedEqualityTest => bin.replace_op(T![==]).map(|it| it.into()),
214 ast::BinOp::EqualityTest => bin.replace_op(T![!=]).map(|it| it.into()), 214 ast::BinOp::EqualityTest => bin.replace_op(T![!=]).map(|it| it.into()),
215 // Parenthesize composite boolean expressions before prefixing `!`
216 ast::BinOp::BooleanAnd | ast::BinOp::BooleanOr => {
217 Some(make::expr_prefix(T![!], make::expr_paren(expr.clone())))
218 }
215 _ => None, 219 _ => None,
216 }, 220 },
217 ast::Expr::MethodCallExpr(mce) => { 221 ast::Expr::MethodCallExpr(mce) => {