diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/numeric/PointlessArithmeticExpressionInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/numeric/PointlessArithmeticExpressionInspection.java index 6b2f34b1aa47..bebf5a559d15 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/numeric/PointlessArithmeticExpressionInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/numeric/PointlessArithmeticExpressionInspection.java @@ -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; - } } \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/numeric/pointless_arithmetic_expression/PointlessArithmeticExpression.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/numeric/pointless_arithmetic_expression/PointlessArithmeticExpression.java index d7577b1c9c79..019805adcf17 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/numeric/pointless_arithmetic_expression/PointlessArithmeticExpression.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/numeric/pointless_arithmetic_expression/PointlessArithmeticExpression.java @@ -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 }} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/numeric/pointless_arithmetic_expression/expected.xml b/plugins/InspectionGadgets/test/com/siyeh/igtest/numeric/pointless_arithmetic_expression/expected.xml index 58e0898bdcdd..f6af12237ea0 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/numeric/pointless_arithmetic_expression/expected.xml +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/numeric/pointless_arithmetic_expression/expected.xml @@ -36,90 +36,6 @@ <code>j%1</code> can be replaced with '0' #loc - - PointlessArithmeticExpression.java - 30 - Pointless arithmetic expression - <code>k<=Integer.MAX_VALUE</code> can be replaced with 'true' #loc - - - - PointlessArithmeticExpression.java - 34 - Pointless arithmetic expression - <code>k>=Integer.MIN_VALUE</code> can be replaced with 'true' #loc - - - - PointlessArithmeticExpression.java - 38 - Pointless arithmetic expression - <code>k>Integer.MAX_VALUE</code> can be replaced with 'false' #loc - - - - PointlessArithmeticExpression.java - 42 - Pointless arithmetic expression - <code>k<Integer.MIN_VALUE</code> can be replaced with 'false' #loc - - - - PointlessArithmeticExpression.java - 46 - Pointless arithmetic expression - <code>Integer.MAX_VALUE >= k</code> can be replaced with 'true' #loc - - - - PointlessArithmeticExpression.java - 50 - Pointless arithmetic expression - <code>Integer.MIN_VALUE <= k</code> can be replaced with 'true' #loc - - - - PointlessArithmeticExpression.java - 54 - Pointless arithmetic expression - <code>Integer.MAX_VALUE < k</code> can be replaced with 'false' #loc - - - - PointlessArithmeticExpression.java - 58 - Pointless arithmetic expression - <code>Integer.MIN_VALUE > k</code> can be replaced with 'false' #loc - - - - PointlessArithmeticExpression.java - 70 - Pointless arithmetic expression - <code>i > Integer.MAX_VALUE</code> can be replaced with 'false' #loc - - - - PointlessArithmeticExpression.java - 76 - Pointless arithmetic expression - <code>i <= Integer.MAX_VALUE</code> can be replaced with 'true' #loc - - - - PointlessArithmeticExpression.java - 80 - Pointless arithmetic expression - <code>i >= Integer.MIN_VALUE</code> can be replaced with 'true' #loc - - - - PointlessArithmeticExpression.java - 83 - Pointless arithmetic expression - <code>i < Integer.MIN_VALUE</code> can be replaced with 'false' #loc - - PointlessArithmeticExpression.java 93