Really fix IDEA-75562

This commit is contained in:
Bas Leijdekkers
2011-10-18 16:02:57 +02:00
parent 55dfb9d0ac
commit 5a935bfb1e
4 changed files with 37 additions and 1 deletions
@@ -185,7 +185,7 @@ public class ReplaceAssignmentWithOperatorAssignmentInspection extends BaseInspe
if (operands.length < 2) {
return;
}
if (operands.length > 2 && !ParenthesesUtils.isCommutativeBinaryOperator(polyadicExpression.getOperationTokenType())) {
if (operands.length > 2 && !ParenthesesUtils.isAssociativeOperation(polyadicExpression)) {
return;
}
for (PsiExpression operand : operands) {
@@ -96,6 +96,32 @@ public class ParenthesesUtils {
token.equals(JavaTokenType.GTGTGT));
}
public static boolean isAssociativeOperation(PsiPolyadicExpression expression) {
final IElementType tokenType = expression.getOperationTokenType();
final PsiType type = expression.getType();
final PsiPrimitiveType primitiveType;
if (type instanceof PsiClassType) {
primitiveType = PsiPrimitiveType.getUnboxedType(type);
if (primitiveType == null) {
return false;
}
} else if (type instanceof PsiPrimitiveType) {
primitiveType = (PsiPrimitiveType)type;
} else {
return false;
}
if (JavaTokenType.PLUS == tokenType || JavaTokenType.ASTERISK == tokenType) {
return primitiveType != PsiType.FLOAT && primitiveType != PsiType.DOUBLE;
} else if (JavaTokenType.EQEQ == tokenType || JavaTokenType.NE == tokenType) {
return primitiveType == PsiType.BOOLEAN;
} else if (JavaTokenType.AND == tokenType || JavaTokenType.OR == tokenType || JavaTokenType.XOR == tokenType) {
return true;
} else if (JavaTokenType.OROR == tokenType || JavaTokenType.ANDAND == tokenType) {
return true;
}
return false;
}
public static int getPrecedence(PsiExpression expression) {
if (expression instanceof PsiThisExpression ||
expression instanceof PsiLiteralExpression ||
@@ -24,5 +24,8 @@ public class ReplaceAssignmentWithOperatorAssignment
x = x / 2 / 4;
x = x >> 1 >> 1;
x = x * 2 * 2;
float f = 1;
f = f * 2 * 2;
}
}
@@ -34,4 +34,11 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Assignment replaceable with operator assignment</problem_class>
<description>&lt;code&gt;x = x + 2 + 2 + 2&lt;/code&gt; could be simplified to 'x += 2 + 2 + 2' #loc</description>
</problem>
<problem>
<file>ReplaceAssignmentWithOperatorAssignment.java</file>
<line>27</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Assignment replaceable with operator assignment</problem_class>
<description>&lt;code&gt;x = x * 2 * 2&lt;/code&gt; could be simplified to 'x *= 2 * 2' #loc</description>
</problem>
</problems>