computeValue moved to CommonDataflow

GitOrigin-RevId: d0b0c9f10eda163f6fd3e7328d942422b6261895
This commit is contained in:
Tagir Valeev
2019-12-18 04:34:21 +00:00
committed by intellij-monorepo-bot
parent 2001b7bfb3
commit 62c68622f1
3 changed files with 21 additions and 20 deletions
@@ -11,6 +11,7 @@ import com.intellij.openapi.util.TextRange;
import com.intellij.psi.*;
import com.intellij.psi.util.*;
import com.intellij.util.JavaPsiConstructorUtil;
import com.intellij.util.containers.ContainerUtil;
import com.siyeh.ig.psiutils.ExpressionUtils;
import one.util.streamex.StreamEx;
import org.jetbrains.annotations.Contract;
@@ -267,6 +268,25 @@ public class CommonDataflow {
return dfType instanceof DfIntegralType ? ((DfIntegralType)dfType).getRange() : null;
}
/**
* Returns the value of given expression calculated via dataflow; or null if value is null or unknown.
*
* @param expression expression to analyze
* @return expression value if known
*/
@Contract("null -> null")
public static Object computeValue(@Nullable PsiExpression expression) {
PsiExpression expressionToAnalyze = PsiUtil.skipParenthesizedExprDown(expression);
if (expressionToAnalyze == null) return null;
Object computed = ExpressionUtils.computeConstantExpression(expressionToAnalyze);
if (computed != null) return computed;
DataflowResult dataflowResult = getDataflowResult(expressionToAnalyze);
if (dataflowResult != null) {
return ContainerUtil.getOnlyItem(dataflowResult.getExpressionValues(expressionToAnalyze));
}
return null;
}
private static class CommonDataflowVisitor extends StandardInstructionVisitor {
private DataflowResult myResult = new DataflowResult(RunnerResult.OK);
private final List<DfaMemoryState> myEndOfInitializerStates = new ArrayList<>();
@@ -322,25 +322,6 @@ public class DfaUtil {
return value;
}
/**
* Returns the value of given expression calculated via dataflow; or null if value is null or unknown.
* The expression context is not taken into account; only expression itself.
*
* @param expression expression to analyze
* @return expression value if known
*/
public static Object computeValue(PsiExpression expression) {
PsiExpression expressionToAnalyze = PsiUtil.skipParenthesizedExprDown(expression);
if (expressionToAnalyze == null) return null;
Object computed = ExpressionUtils.computeConstantExpression(expressionToAnalyze);
if (computed != null) return computed;
CommonDataflow.DataflowResult dataflowResult = CommonDataflow.getDataflowResult(expressionToAnalyze);
if (dataflowResult != null) {
return ContainerUtil.getOnlyItem(dataflowResult.getExpressionValues(expressionToAnalyze));
}
return null;
}
@NotNull
public static List<? extends MethodContract> addRangeContracts(@Nullable PsiMethod method,
@NotNull List<? extends MethodContract> contracts) {
@@ -109,7 +109,7 @@ public class ConditionCoveredByFurtherConditionInspection extends AbstractBaseJa
private static int[] getRedundantOperandIndices(PsiPolyadicExpression context, List<PsiExpression> operands, boolean and) {
assert !operands.isEmpty();
if (operands.size() == 1) {
Object value = DfaUtil.computeValue(operands.get(0));
Object value = CommonDataflow.computeValue(operands.get(0));
return Boolean.valueOf(and).equals(value) ? new int[]{0} : ArrayUtilRt.EMPTY_INT_ARRAY;
}
String text = StreamEx.ofReversed(operands)