mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-143315 Suppress zero test extracted; suppress method call result if all args are constants
Review ID: IDEA-CR-60202 GitOrigin-RevId: 278d64a98d03fb9f9f2456e8cf8212b969c7d4de
This commit is contained in:
committed by
intellij-monorepo-bot
parent
d2edc0a0e8
commit
c308f0b8c0
+25
-11
@@ -40,6 +40,7 @@ import javax.swing.*;
|
||||
import java.text.MessageFormat;
|
||||
import java.util.*;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static com.intellij.util.ObjectUtils.tryCast;
|
||||
|
||||
@@ -454,23 +455,36 @@ public abstract class DataFlowInspectionBase extends AbstractBaseJavaLocalInspec
|
||||
}
|
||||
|
||||
private static boolean shouldReportZero(PsiExpression ref) {
|
||||
boolean reportZeroValue = ref instanceof PsiPolyadicExpression && !PsiUtil.isConstantExpression(ref) ||
|
||||
ref instanceof PsiMethodCallExpression;
|
||||
if (!reportZeroValue) return false;
|
||||
if (ref instanceof PsiPolyadicExpression) {
|
||||
if (PsiUtil.isConstantExpression(ref)) return false;
|
||||
PsiPolyadicExpression polyadic = (PsiPolyadicExpression)ref;
|
||||
IElementType tokenType = polyadic.getOperationTokenType();
|
||||
if (tokenType.equals(JavaTokenType.ASTERISK)) {
|
||||
PsiMethod method = PsiTreeUtil.getParentOfType(ref, PsiMethod.class, true, PsiLambdaExpression.class, PsiClass.class);
|
||||
if (MethodUtils.isHashCode(method)) {
|
||||
// Standard hashCode template generates int result = 0; result = result * 31 + ...;
|
||||
// so annoying warnings might be produced there
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (ref instanceof PsiMethodCallExpression) {
|
||||
PsiMethodCallExpression call = (PsiMethodCallExpression)ref;
|
||||
PsiExpression qualifier = call.getMethodExpression().getQualifierExpression();
|
||||
if (PsiUtil.isConstantExpression(qualifier) &&
|
||||
Stream.of(call.getArgumentList().getExpressions()).allMatch(PsiUtil::isConstantExpression)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
PsiElement parent = PsiUtil.skipParenthesizedExprUp(ref.getParent());
|
||||
PsiBinaryExpression binOp = tryCast(parent, PsiBinaryExpression.class);
|
||||
if (binOp != null && ComparisonUtils.isEqualityComparison(binOp) &&
|
||||
(ExpressionUtils.isZero(binOp.getLOperand()) || ExpressionUtils.isZero(binOp.getROperand()))) {
|
||||
return false;
|
||||
}
|
||||
PsiMethod method = PsiTreeUtil.getParentOfType(parent, PsiMethod.class, true, PsiLambdaExpression.class, PsiClass.class);
|
||||
if (MethodUtils.isHashCode(method)) {
|
||||
// Standard hashCode template generates int result = 0; result = result * 31 + ...;
|
||||
// so annoying warnings might be produced there
|
||||
if (ref instanceof PsiPolyadicExpression && ((PsiPolyadicExpression)ref).getOperationTokenType().equals(JavaTokenType.ASTERISK)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -229,16 +229,4 @@ public class LongRangeBasics {
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
class HashCode {
|
||||
Object a, b, c;
|
||||
|
||||
public int hashCode() {
|
||||
int result = 0;
|
||||
result = result * 31 + a.hashCode();
|
||||
result = result * 31 + b.hashCode();
|
||||
result = result * 31 + c.hashCode();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
class HashCode {
|
||||
Object a, b, c;
|
||||
|
||||
public int hashCode() {
|
||||
int result = 0;
|
||||
result = result * 31 + a.hashCode();
|
||||
result = result * 31 + b.hashCode();
|
||||
result = result * 31 + c.hashCode();
|
||||
return result;
|
||||
}
|
||||
|
||||
public int hashCode2() {
|
||||
int result = 0;
|
||||
result = <warning descr="Result of 'result * 31' is always '0'">result * 31</warning> + a.hashCode();
|
||||
result = result * 31 + b.hashCode();
|
||||
result = result * 31 + c.hashCode();
|
||||
return result;
|
||||
}
|
||||
|
||||
static final String STRING = "123456";
|
||||
static final String PREFIX = "123";
|
||||
|
||||
void testIndexOf(String s) {
|
||||
int pos = STRING.indexOf(PREFIX);
|
||||
if (s.equals(PREFIX)) {
|
||||
int pos2 = <warning descr="Result of 's.indexOf(PREFIX)' is always '0'">s.indexOf(PREFIX)</warning>;
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
@@ -70,4 +70,5 @@ public class DataFlowRangeAnalysisTest extends DataFlowInspectionTestCase {
|
||||
public void testWidenPlusInLoop() { doTest(); }
|
||||
public void testWidenMulInLoop() { doTest(); }
|
||||
public void testReduceBinOpOnCast() { doTest(); }
|
||||
public void testSuppressZeroReport() { doTest(); }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user