don't report on incomplete code or comparisons that are already reported by "Constant conditions & exceptions"

This commit is contained in:
Bas Leijdekkers
2014-04-21 17:25:28 +02:00
parent 6e6811147a
commit 448e9f8d02
3 changed files with 10 additions and 172 deletions
@@ -20,8 +20,8 @@ import com.intellij.codeInspection.ui.SingleCheckboxOptionsPanel;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.tree.IElementType;
import com.intellij.psi.util.ConstantExpressionUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.psi.util.PsiUtilCore;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
@@ -116,20 +116,19 @@ public class PointlessArithmeticExpressionInspection
}
else if (tokenType.equals(JavaTokenType.ASTERISK) && isZero(operand) ||
tokenType.equals(JavaTokenType.PERC) && (isOne(operand) || EquivalenceChecker.expressionsAreEquivalent(previousOperand, operand))) {
return PsiType.LONG.equals(polyadicExpression.getType()) ? "0L" : "0";
}
else if (tokenType.equals(JavaTokenType.LE) || tokenType.equals(JavaTokenType.GE) ||
tokenType.equals(JavaTokenType.LT) || tokenType.equals(JavaTokenType.GT)) {
return (tokenType.equals(JavaTokenType.LT) || tokenType.equals(JavaTokenType.GT)) ? "false" : "true";
fromTarget = operands[0];
untilTarget = operands[length - 1];
replacement = PsiType.LONG.equals(polyadicExpression.getType()) ? "0L" : "0";
break;
}
previousOperand = operand;
}
return buildReplacementExpression(polyadicExpression, fromTarget, untilTarget, replacement).trim();
return getText(polyadicExpression, fromTarget, untilTarget, replacement).trim();
}
public static String buildReplacementExpression(PsiPolyadicExpression expression, PsiElement fromTarget, PsiElement untilTarget,
String replacement) {
public static String getText(PsiPolyadicExpression expression, PsiElement fromTarget, PsiElement untilTarget,
@NotNull @NonNls String replacement) {
final StringBuilder result = new StringBuilder();
boolean stop = false;
for (PsiElement child : expression.getChildren()) {
@@ -198,13 +197,10 @@ public class PointlessArithmeticExpressionInspection
if (!arithmeticTokens.contains(expression.getOperationTokenType())) {
return;
}
if (ExpressionUtils.hasStringType(expression)) {
if (ExpressionUtils.hasStringType(expression) || PsiUtilCore.hasErrorElementChild(expression)) {
return;
}
final PsiExpression[] operands = expression.getOperands();
if (operands.length < 2) {
return;
}
final IElementType tokenType = expression.getOperationTokenType();
final boolean isPointless;
if (tokenType.equals(JavaTokenType.PLUS)) {
@@ -222,14 +218,6 @@ public class PointlessArithmeticExpressionInspection
else if (tokenType.equals(JavaTokenType.PERC)) {
isPointless = modExpressionIsPointless(operands);
}
else if (tokenType.equals(JavaTokenType.LE) ||
tokenType.equals(JavaTokenType.GE) ||
tokenType.equals(JavaTokenType.GT) ||
tokenType.equals(JavaTokenType.LT)) {
final PsiExpression lhs = operands[0];
final PsiExpression rhs = operands[1];
isPointless = comparisonExpressionIsPointless(lhs, rhs, tokenType);
}
else {
isPointless = false;
}
@@ -292,45 +280,6 @@ public class PointlessArithmeticExpressionInspection
}
return false;
}
private boolean comparisonExpressionIsPointless(
PsiExpression lhs, PsiExpression rhs, IElementType comparison) {
if (PsiType.INT.equals(lhs.getType()) &&
PsiType.INT.equals(rhs.getType())) {
return intComparisonIsPointless(lhs, rhs, comparison);
}
else if (PsiType.LONG.equals(lhs.getType()) &&
PsiType.LONG.equals(rhs.getType())) {
return longComparisonIsPointless(lhs, rhs, comparison);
}
return false;
}
private boolean intComparisonIsPointless(
PsiExpression lhs, PsiExpression rhs, IElementType comparison) {
if (isMaxInt(lhs) || isMinInt(rhs)) {
return JavaTokenType.GE.equals(comparison) ||
JavaTokenType.LT.equals(comparison);
}
if (isMinInt(lhs) || isMaxInt(rhs)) {
return JavaTokenType.LE.equals(comparison) ||
JavaTokenType.GT.equals(comparison);
}
return false;
}
private boolean longComparisonIsPointless(
PsiExpression lhs, PsiExpression rhs, IElementType comparison) {
if (isMaxLong(lhs) || isMinLong(rhs)) {
return JavaTokenType.GE.equals(comparison) ||
JavaTokenType.LT.equals(comparison);
}
if (isMinLong(lhs) || isMaxLong(rhs)) {
return JavaTokenType.LE.equals(comparison) ||
JavaTokenType.GT.equals(comparison);
}
return false;
}
}
boolean isZero(PsiExpression expression) {
@@ -346,32 +295,4 @@ public class PointlessArithmeticExpressionInspection
}
return ExpressionUtils.isOne(expression);
}
private static boolean isMinInt(PsiExpression expression) {
final Integer value = (Integer)
ConstantExpressionUtil.computeCastTo(
expression, PsiType.INT);
return value != null && value.intValue() == Integer.MIN_VALUE;
}
private static boolean isMaxInt(PsiExpression expression) {
final Integer value = (Integer)
ConstantExpressionUtil.computeCastTo(
expression, PsiType.INT);
return value != null && value.intValue() == Integer.MAX_VALUE;
}
private static boolean isMinLong(PsiExpression expression) {
final Long value = (Long)
ConstantExpressionUtil.computeCastTo(
expression, PsiType.LONG);
return value != null && value.longValue() == Long.MIN_VALUE;
}
private static boolean isMaxLong(PsiExpression expression) {
final Long value = (Long)
ConstantExpressionUtil.computeCastTo(
expression, PsiType.LONG);
return value != null && value.longValue() == Long.MAX_VALUE;
}
}
@@ -123,4 +123,5 @@ class Expanded {{
System.out.println(u * 1);
long g = 8L / 8L;
long h = 9L * 0L;
int a = 8 * 0 * 8 * ; // don't warn
}}
@@ -36,90 +36,6 @@
<description>&lt;code&gt;j%1&lt;/code&gt; can be replaced with '0' #loc</description>
</problem>
<problem>
<file>PointlessArithmeticExpression.java</file>
<line>30</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Pointless arithmetic expression</problem_class>
<description>&lt;code&gt;k&lt;=Integer.MAX_VALUE&lt;/code&gt; can be replaced with 'true' #loc</description>
</problem>
<problem>
<file>PointlessArithmeticExpression.java</file>
<line>34</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Pointless arithmetic expression</problem_class>
<description>&lt;code&gt;k&gt;=Integer.MIN_VALUE&lt;/code&gt; can be replaced with 'true' #loc</description>
</problem>
<problem>
<file>PointlessArithmeticExpression.java</file>
<line>38</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Pointless arithmetic expression</problem_class>
<description>&lt;code&gt;k&gt;Integer.MAX_VALUE&lt;/code&gt; can be replaced with 'false' #loc</description>
</problem>
<problem>
<file>PointlessArithmeticExpression.java</file>
<line>42</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Pointless arithmetic expression</problem_class>
<description>&lt;code&gt;k&lt;Integer.MIN_VALUE&lt;/code&gt; can be replaced with 'false' #loc</description>
</problem>
<problem>
<file>PointlessArithmeticExpression.java</file>
<line>46</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Pointless arithmetic expression</problem_class>
<description>&lt;code&gt;Integer.MAX_VALUE &gt;= k&lt;/code&gt; can be replaced with 'true' #loc</description>
</problem>
<problem>
<file>PointlessArithmeticExpression.java</file>
<line>50</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Pointless arithmetic expression</problem_class>
<description>&lt;code&gt;Integer.MIN_VALUE &lt;= k&lt;/code&gt; can be replaced with 'true' #loc</description>
</problem>
<problem>
<file>PointlessArithmeticExpression.java</file>
<line>54</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Pointless arithmetic expression</problem_class>
<description>&lt;code&gt;Integer.MAX_VALUE &lt; k&lt;/code&gt; can be replaced with 'false' #loc</description>
</problem>
<problem>
<file>PointlessArithmeticExpression.java</file>
<line>58</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Pointless arithmetic expression</problem_class>
<description>&lt;code&gt;Integer.MIN_VALUE &gt; k&lt;/code&gt; can be replaced with 'false' #loc</description>
</problem>
<problem>
<file>PointlessArithmeticExpression.java</file>
<line>70</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Pointless arithmetic expression</problem_class>
<description>&lt;code&gt;i &gt; Integer.MAX_VALUE&lt;/code&gt; can be replaced with 'false' #loc</description>
</problem>
<problem>
<file>PointlessArithmeticExpression.java</file>
<line>76</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Pointless arithmetic expression</problem_class>
<description>&lt;code&gt;i &lt;= Integer.MAX_VALUE&lt;/code&gt; can be replaced with 'true' #loc</description>
</problem>
<problem>
<file>PointlessArithmeticExpression.java</file>
<line>80</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Pointless arithmetic expression</problem_class>
<description>&lt;code&gt;i &gt;= Integer.MIN_VALUE&lt;/code&gt; can be replaced with 'true' #loc</description>
</problem>
<problem>
<file>PointlessArithmeticExpression.java</file>
<line>83</line>
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Pointless arithmetic expression</problem_class>
<description>&lt;code&gt;i &lt; Integer.MIN_VALUE&lt;/code&gt; can be replaced with 'false' #loc</description>
</problem>
<problem>
<file>PointlessArithmeticExpression.java</file>
<line>93</line>