diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-12-16 08:20:11 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2020-12-16 08:20:11 +0000 |
commit | ece626fe81579b6b38cdbd17d3e47bb422360a56 (patch) | |
tree | 3d7936e0c994fbf931085fd7e4dc1eb203d2cb54 /crates | |
parent | eb9ba457b0126bd45c45ef751e92ccfdc5fdc206 (diff) | |
parent | 0f42a71806ad62cd042dd40ab42501180fb72999 (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')
-rw-r--r-- | crates/assists/src/handlers/invert_if.rs | 9 | ||||
-rw-r--r-- | crates/assists/src/utils.rs | 4 | ||||
-rw-r--r-- | crates/syntax/src/ast/make.rs | 3 |
3 files changed, 16 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) => { |
diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs index cc09b77a5..16b079c42 100644 --- a/crates/syntax/src/ast/make.rs +++ b/crates/syntax/src/ast/make.rs | |||
@@ -196,6 +196,9 @@ pub fn expr_method_call(receiver: ast::Expr, method: &str, arg_list: ast::ArgLis | |||
196 | pub fn expr_ref(expr: ast::Expr, exclusive: bool) -> ast::Expr { | 196 | pub fn expr_ref(expr: ast::Expr, exclusive: bool) -> ast::Expr { |
197 | expr_from_text(&if exclusive { format!("&mut {}", expr) } else { format!("&{}", expr) }) | 197 | expr_from_text(&if exclusive { format!("&mut {}", expr) } else { format!("&{}", expr) }) |
198 | } | 198 | } |
199 | pub fn expr_paren(expr: ast::Expr) -> ast::Expr { | ||
200 | expr_from_text(&format!("({})", expr)) | ||
201 | } | ||
199 | fn expr_from_text(text: &str) -> ast::Expr { | 202 | fn expr_from_text(text: &str) -> ast::Expr { |
200 | ast_from_text(&format!("const C: () = {};", text)) | 203 | ast_from_text(&format!("const C: () = {};", text)) |
201 | } | 204 | } |