mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-182645 Comparator does not return 0: do not report for always failing comparator
This commit is contained in:
+14
@@ -24,6 +24,7 @@ import com.intellij.codeInspection.dataFlow.value.DfaVariableValue;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.util.ObjectUtils;
|
||||
import com.siyeh.InspectionGadgetsBundle;
|
||||
import com.siyeh.ig.BaseInspection;
|
||||
import com.siyeh.ig.BaseInspectionVisitor;
|
||||
@@ -96,6 +97,19 @@ public class SuspiciousComparatorCompareInspection extends BaseInspection {
|
||||
PsiStatement statement = ControlFlowUtils.getOnlyStatementInBlock((PsiCodeBlock)body);
|
||||
if (statement instanceof PsiReturnStatement && ExpressionUtils.isZero(((PsiReturnStatement)statement).getReturnValue())) return;
|
||||
}
|
||||
PsiMethodCallExpression soleCall = ObjectUtils.tryCast(LambdaUtil.extractSingleExpressionFromBody(body), PsiMethodCallExpression.class);
|
||||
if (soleCall != null) {
|
||||
PsiMethod method = soleCall.resolveMethod();
|
||||
if (method != null) {
|
||||
List<? extends MethodContract> contracts = ControlFlowAnalyzer.getMethodCallContracts(method, soleCall);
|
||||
if (contracts.size() == 1) {
|
||||
MethodContract contract = contracts.get(0);
|
||||
if (contract.isTrivial() && contract.getReturnValue() == MethodContract.ValueConstraint.THROW_EXCEPTION) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
PsiParameter[] parameters = parameterList.getParameters();
|
||||
checkParameterList(parameters, body);
|
||||
checkReflexivity(parameters, body);
|
||||
|
||||
+6
@@ -27,4 +27,10 @@ class ComparatorIsNotReflexive implements Comparator<Integer> {
|
||||
if(b1.length != b2.length) return 0; // typo: == was intended
|
||||
return <warning descr="Comparator does not return 0 for equal elements">b1.length > b2.length ? 1 : -1</warning>;
|
||||
};
|
||||
|
||||
Comparator<String> cmp = (a,b) -> test();
|
||||
|
||||
static int test() {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user