Bug 568822 - Expressions with two repeated operands should not be
refactored
Let be the following case:
(X && Y) || (!X && !Y)
If you run both "Ternary operator" and "Use '^' or '==' on booleans"
rules, you don't know if you will get this:
(X ? Y : !Y)
...or this:
X == Y
...because both rules match the same code. They are in conflict and the
result is a random result.
So we should disable the matching for "Ternary operator" rule because
the result is worse.
Change-Id: I84a2e436060942998a9d1d1677129acc28a16a9d
Signed-off-by: Fabrice Tiercelin <fabrice.tiercelin@yahoo.fr>
diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest.java
index 701072e..98a3312 100644
--- a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest.java
+++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest.java
@@ -5996,6 +5996,14 @@
+ " boolean newBoolean1 = b1 && b2 || !b1 && b3 && b4;\n" //
+ " }\n" //
+ "\n" //
+ + " public void doNoUseTernaryOperatorWithSameExpressions(boolean b1, int number) {\n" //
+ + " boolean newBoolean1 = b1 && (number > 0) || !b1 && (0 < number);\n" //
+ + " }\n" //
+ + "\n" //
+ + " public void doNoUseTernaryOperatorWithNegativeExpressions(boolean b1, int number) {\n" //
+ + " boolean newBoolean1 = b1 && (number > 0) || !b1 && (0 >= number);\n" //
+ + " }\n" //
+ + "\n" //
+ " public void doNoReplaceDuplicateConditionsWithWrappers(Boolean b1, Boolean b2, Boolean b3) {\n" //
+ " boolean newBoolean1 = b1 && b2 || !b1 && b3;\n" //
+ " boolean newBoolean2 = b1 && !b2 || !b1 && b3;\n" //
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/fix/TernaryOperatorCleanUp.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/fix/TernaryOperatorCleanUp.java
index 85c3286..5ffe912 100644
--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/fix/TernaryOperatorCleanUp.java
+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/fix/TernaryOperatorCleanUp.java
@@ -143,7 +143,8 @@
final Expression oppositeCondition, final Expression oneExpression, final Expression oppositeExpression,
final List<Expression> previousOperands, final List<Expression> nextOperands) {
if (ASTSemanticMatcher.INSTANCE.matchNegative(oneCondition, oppositeCondition)
- && !ASTNodes.match(oneExpression, oppositeExpression)) {
+ && !ASTNodes.match(oneExpression, oppositeExpression)
+ && !ASTSemanticMatcher.INSTANCE.matchNegative(oneExpression, oppositeExpression)) {
rewriteOperations.add(new TernaryOperatorOperation(visited, oneCondition, oneExpression, oppositeExpression, previousOperands,
nextOperands));
return false;