IG: a != b and b != a and other commutative binary operations are equivalent

This commit is contained in:
Bas Leijdekkers
2017-09-20 23:02:58 +02:00
parent 0be23bc144
commit 3db80a01df
3 changed files with 24 additions and 9 deletions
@@ -899,14 +899,17 @@ public class EquivalenceChecker {
final PsiExpression right2 = binaryExpression2.getROperand();
if (!tokenType1.equals(tokenType2)) {
// process matches like "a < b" and "b > a"
DfaRelationValue.RelationType rel1 = DfaRelationValue.RelationType.fromElementType(tokenType1);
DfaRelationValue.RelationType rel2 = DfaRelationValue.RelationType.fromElementType(tokenType2);
final DfaRelationValue.RelationType rel1 = DfaRelationValue.RelationType.fromElementType(tokenType1);
final DfaRelationValue.RelationType rel2 = DfaRelationValue.RelationType.fromElementType(tokenType2);
if(rel1 != null && rel2 != null && rel1.getFlipped() == rel2) {
return expressionsAreEquivalent(new PsiExpression[] {left1, right1}, new PsiExpression[] {right2, left2});
}
return EXACT_MISMATCH;
}
return expressionsAreEquivalent(new PsiExpression[] {left1, right1}, new PsiExpression[] {left2, right2});
final Match match = expressionsAreEquivalent(new PsiExpression[] {left1, right1}, new PsiExpression[] {left2, right2});
return match == EXACT_MISMATCH && ParenthesesUtils.isCommutativeOperation(binaryExpression1)
? expressionsAreEquivalent(new PsiExpression[]{left1, right1}, new PsiExpression[]{right2, left2})
: match;
}
protected Match assignmentExpressionsMatch(@NotNull PsiAssignmentExpression assignmentExpression1, @NotNull PsiAssignmentExpression assignmentExpression2) {
@@ -100,12 +100,18 @@ public class ParenthesesUtils {
}
public static boolean isCommutativeOperator(@NotNull IElementType token) {
return !(token.equals(JavaTokenType.MINUS) ||
token.equals(JavaTokenType.DIV) ||
token.equals(JavaTokenType.PERC) ||
token.equals(JavaTokenType.LTLT) ||
token.equals(JavaTokenType.GTGT) ||
token.equals(JavaTokenType.GTGTGT));
return token == JavaTokenType.PLUS || token == JavaTokenType.ASTERISK ||
token == JavaTokenType.EQEQ || token == JavaTokenType.NE ||
token == JavaTokenType.AND || token == JavaTokenType.OR || token == JavaTokenType.XOR;
}
public static boolean isCommutativeOperation(PsiPolyadicExpression expression) {
final IElementType tokenType = expression.getOperationTokenType();
if (!isCommutativeOperator(tokenType)) {
return false;
}
final PsiType type = expression.getType();
return type != null && !type.equalsToText(CommonClassNames.JAVA_LANG_STRING);
}
public static boolean isAssociativeOperation(PsiPolyadicExpression expression) {
@@ -33,6 +33,12 @@ public class DuplicateCondition {
System.out.println("second");
return;
}
if (<warning descr="Duplicate condition 'x != y'">x != y</warning>) {
return;
}
if ((<warning descr="Duplicate condition '(y) != x'">(y) != x</warning>)) {
return;
}
}
public void foo()