diff options
| author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-12-21 16:57:49 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-12-21 16:57:49 +0000 |
| commit | c8a73fe655f7bf9778deeec4c8b4b541e0af398b (patch) | |
| tree | f6f9a11b950e001505a3c711f59d34620a037684 | |
| parent | 71c8073aa1e2cd3101a031a4f10e460013d4df74 (diff) | |
| parent | 2e7abf83844ef0c7d807262a682941f2aef9d3d9 (diff) | |
| download | rust-c8a73fe655f7bf9778deeec4c8b4b541e0af398b.tar.gz rust-c8a73fe655f7bf9778deeec4c8b4b541e0af398b.zip | |
Merge #6982
6982: Remove parentheses when inverting `!(cond)` r=matklad a=Jesse-Bakker Followup to #6894 When inverting a composite condition twice, the parentheses were left. This also removes those unnecessary parentheses when applying the invert-if assist. Co-authored-by: Jesse Bakker <github@jessebakker.com>
| -rw-r--r-- | crates/assists/src/handlers/invert_if.rs | 9 | ||||
| -rw-r--r-- | crates/assists/src/utils.rs | 8 |
2 files changed, 16 insertions, 1 deletions
diff --git a/crates/assists/src/handlers/invert_if.rs b/crates/assists/src/handlers/invert_if.rs index 91e2f5c8cb3..f9c33b3f7d1 100644 --- a/crates/assists/src/handlers/invert_if.rs +++ b/crates/assists/src/handlers/invert_if.rs @@ -78,6 +78,15 @@ mod tests { } #[test] + fn invert_if_remove_not_parentheses() { + check_assist( + invert_if, + "fn f() { i<|>f !(x == 3 || x == 4 || x == 5) { 3 * 2 } else { 1 } }", + "fn f() { if x == 3 || x == 4 || x == 5 { 1 } else { 3 * 2 } }", + ) + } + + #[test] fn invert_if_remove_inequality() { check_assist( invert_if, diff --git a/crates/assists/src/utils.rs b/crates/assists/src/utils.rs index f2cacf7c80a..d41084b5994 100644 --- a/crates/assists/src/utils.rs +++ b/crates/assists/src/utils.rs @@ -232,7 +232,13 @@ fn invert_special_case(expr: &ast::Expr) -> Option<ast::Expr> { }; Some(make::expr_method_call(receiver, method, arg_list)) } - ast::Expr::PrefixExpr(pe) if pe.op_kind()? == ast::PrefixOp::Not => pe.expr(), + ast::Expr::PrefixExpr(pe) if pe.op_kind()? == ast::PrefixOp::Not => { + if let ast::Expr::ParenExpr(parexpr) = pe.expr()? { + parexpr.expr() + } else { + pe.expr() + } + } // FIXME: // ast::Expr::Literal(true | false ) _ => None, |
