IG: fix "Use of variable whose value is known to be constant" inspection in the presence of polyadic expressions

This commit is contained in:
Bas Leijdekkers
2015-07-06 16:24:22 +02:00
parent fe1374be84
commit 40a3b20ed4
2 changed files with 22 additions and 12 deletions
@@ -26,6 +26,7 @@ import com.siyeh.ig.BaseInspectionVisitor;
import com.siyeh.ig.InspectionGadgetsFix;
import com.siyeh.ig.PsiReplacementUtil;
import com.siyeh.ig.psiutils.ExpressionUtils;
import com.siyeh.ig.psiutils.ParenthesesUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -58,7 +59,6 @@ public class ConstantValueVariableUseInspection extends BaseInspection {
myText = text;
}
@Override
@NotNull
public String getName() {
@@ -121,25 +121,28 @@ public class ConstantValueVariableUseInspection extends BaseInspection {
if (body == null) {
return false;
}
if (!(condition instanceof PsiBinaryExpression)) {
if (!(condition instanceof PsiPolyadicExpression)) {
return false;
}
final PsiBinaryExpression binaryExpression =
(PsiBinaryExpression)condition;
final IElementType tokenType =
binaryExpression.getOperationTokenType();
final PsiExpression lhs = binaryExpression.getLOperand();
final PsiExpression rhs = binaryExpression.getROperand();
final PsiPolyadicExpression polyadicExpression = (PsiPolyadicExpression)condition;
final IElementType tokenType = polyadicExpression.getOperationTokenType();
if (JavaTokenType.ANDAND == tokenType) {
return checkCondition(lhs, body) ||
checkCondition(rhs, body);
for (PsiExpression operand : polyadicExpression.getOperands()) {
if (checkCondition(operand, body)) {
return true;
}
}
return false;
}
if (JavaTokenType.EQEQ != tokenType) {
return false;
}
if (rhs == null) {
final PsiExpression[] operands = polyadicExpression.getOperands();
if (operands.length != 2) {
return false;
}
final PsiExpression lhs = operands[0];
final PsiExpression rhs = operands[1];
if (PsiUtil.isConstantExpression(lhs)) {
return checkConstantValueVariableUse(rhs, lhs, body);
}
@@ -162,6 +165,7 @@ public class ConstantValueVariableUseInspection extends BaseInspection {
return false;
}
}
expression = ParenthesesUtils.stripParentheses(expression);
if (!(expression instanceof PsiReferenceExpression)) {
return false;
}
@@ -6,7 +6,7 @@ public class ConstantValueVariableUse {
public static void test(double number) {
if (number == -0.0) { // is true for -0.0
f(number);
}8
}
if (number == 1.0) {
f(<warning descr="Value of 'number' is known to be constant">number</warning>);
}
@@ -16,4 +16,10 @@ public class ConstantValueVariableUse {
System.out.println("signedZero = " + signedZero);
}
void m(int i) {
if ((i) == 2 && true && true) { // polyadic
f(<warning descr="Value of 'i' is known to be constant">i</warning>);
}
}
}