diff --git a/java/java-analysis-impl/src/com/intellij/codeInsight/guess/impl/ExpressionTypeMemoryState.java b/java/java-analysis-impl/src/com/intellij/codeInsight/guess/impl/ExpressionTypeMemoryState.java index 9c6dc22f26a5..b53228d0d8fb 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInsight/guess/impl/ExpressionTypeMemoryState.java +++ b/java/java-analysis-impl/src/com/intellij/codeInsight/guess/impl/ExpressionTypeMemoryState.java @@ -127,7 +127,7 @@ public class ExpressionTypeMemoryState extends DfaMemoryStateImpl { if (!negated) { setExpressionType(value.getExpression(), value.getCastType()); } - return super.applyCondition(negated ? value.getRelation().createNegated() : value.getRelation()); + return super.applyCondition(negated ? value.getRelation().negate() : value.getRelation()); } } } diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/InstructionVisitor.java b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/InstructionVisitor.java index 3bbb297e649e..403e57eab9a5 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/InstructionVisitor.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/InstructionVisitor.java @@ -248,18 +248,8 @@ public abstract class InstructionVisitor { } public DfaInstructionState[] visitConditionalGoto(ConditionalGotoInstruction instruction, DataFlowRunner runner, DfaMemoryState memState) { - DfaCondition cond = memState.pop().eq(runner.getFactory().getConstFactory().getTrue()); - - DfaCondition condTrue; - DfaCondition condFalse; - - if (instruction.isNegated()) { - condFalse = cond; - condTrue = cond.createNegated(); - } else { - condTrue = cond; - condFalse = cond.createNegated(); - } + DfaCondition condTrue = memState.pop().eq(runner.getFactory().getBoolean(!instruction.isNegated())); + DfaCondition condFalse = condTrue.negate(); if (condTrue == DfaCondition.getTrue()) { markBranchReachable(instruction, true); diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/StandardInstructionVisitor.java b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/StandardInstructionVisitor.java index 8a8039e146c5..81a356c6fedb 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/StandardInstructionVisitor.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/StandardInstructionVisitor.java @@ -464,7 +464,7 @@ public class StandardInstructionVisitor extends InstructionVisitor { for (ContractValue contractValue : contract.getConditions()) { DfaCondition condition = contractValue.makeCondition(factory, callState.myCallArguments); DfaMemoryState falseState = state.createCopy(); - DfaCondition falseCondition = condition.createNegated(); + DfaCondition falseCondition = condition.negate(); if (contract.getReturnValue().isFail() ? falseState.applyCondition(falseCondition) : falseState.applyContractCondition(falseCondition)) { @@ -797,7 +797,7 @@ public class StandardInstructionVisitor extends InstructionVisitor { if (equality.applyCondition(condition)) { states.add(makeBooleanResult(instruction, runner, equality, ThreeState.UNSURE)); } - if (memState.applyCondition(condition.createNegated())) { + if (memState.applyCondition(condition.negate())) { states.add(makeBooleanResult(instruction, runner, memState, ThreeState.fromBoolean(relationType == RelationType.NE))); } return states.toArray(DfaInstructionState.EMPTY_ARRAY); @@ -890,7 +890,7 @@ public class StandardInstructionVisitor extends InstructionVisitor { if (trueState.applyCondition(condition)) { states.add(makeBooleanResult(instruction, runner, trueState, unknownTargetType ? ThreeState.UNSURE : ThreeState.YES)); } - DfaCondition negated = condition.createNegated(); + DfaCondition negated = condition.negate(); if (unknownTargetType ? memState.applyContractCondition(negated) : memState.applyCondition(negated)) { states.add(makeBooleanResult(instruction, runner, memState, ThreeState.NO)); useful |= !memState.isNull(dfaLeft); diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/value/DfaCondition.java b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/value/DfaCondition.java index b8a16c50520b..07c748f38289 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/value/DfaCondition.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/value/DfaCondition.java @@ -18,10 +18,13 @@ import org.jetbrains.annotations.Nullable; public abstract class DfaCondition { // To prevent inheritors outside of the package DfaCondition() {} - + + /** + * @return a condition which is the opposite to this condition + */ @NotNull @Contract(pure = true) - public abstract DfaCondition createNegated(); + public abstract DfaCondition negate(); /** * @return always true condition; singleton object @@ -77,7 +80,7 @@ public abstract class DfaCondition { @NotNull @Override - public DfaCondition createNegated() { + public DfaCondition negate() { if (this == TRUE) return FALSE; if (this == FALSE) return TRUE; return UNKNOWN; diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/value/DfaRelation.java b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/value/DfaRelation.java index 7b4eb0856018..6b8f375cbc18 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/value/DfaRelation.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/value/DfaRelation.java @@ -27,8 +27,8 @@ import org.jetbrains.annotations.NotNull; public final class DfaRelation extends DfaCondition { @NotNull @Override - public DfaRelation createNegated() { - return createCanonicalRelation(myLeftOperand, myRelation.getNegated(), myRightOperand); + public DfaRelation negate() { + return new DfaRelation(myLeftOperand, myRightOperand, myRelation.getNegated()); } private @NotNull final DfaValue myLeftOperand; @@ -54,16 +54,16 @@ public final class DfaRelation extends DfaCondition { public static DfaRelation createRelation(@NotNull DfaValue dfaLeft, @NotNull RelationType relationType, @NotNull DfaValue dfaRight) { if ((relationType == RelationType.IS || relationType == RelationType.IS_NOT) && dfaRight instanceof DfaFactMapValue && !(dfaLeft instanceof DfaFactMapValue)) { - return createCanonicalRelation(dfaLeft, relationType, dfaRight); + return new DfaRelation(dfaLeft, dfaRight, relationType); } if (dfaLeft instanceof DfaVariableValue || dfaLeft instanceof DfaBoxedValue || dfaLeft instanceof DfaBinOpValue || dfaRight instanceof DfaVariableValue || dfaRight instanceof DfaBoxedValue || dfaRight instanceof DfaBinOpValue) { if (!(dfaLeft instanceof DfaVariableValue || dfaLeft instanceof DfaBoxedValue || dfaLeft instanceof DfaBinOpValue) || (dfaRight instanceof DfaBinOpValue && !(dfaLeft instanceof DfaBinOpValue))) { RelationType flipped = relationType.getFlipped(); - return flipped == null ? null : createCanonicalRelation(dfaRight, flipped, dfaLeft); + return flipped == null ? null : new DfaRelation(dfaRight, dfaLeft, flipped); } - return createCanonicalRelation(dfaLeft, relationType, dfaRight); + return new DfaRelation(dfaLeft, dfaRight, relationType); } if (dfaLeft instanceof DfaFactMapValue && dfaRight instanceof DfaConstValue) { return createConstBasedRelation((DfaFactMapValue)dfaLeft, relationType, (DfaConstValue)dfaRight); @@ -72,10 +72,10 @@ public final class DfaRelation extends DfaCondition { return createConstBasedRelation((DfaFactMapValue)dfaRight, relationType, (DfaConstValue)dfaLeft); } if (dfaLeft instanceof DfaInstanceofValue && dfaRight instanceof DfaConstValue) { - return createCanonicalRelation(dfaLeft, relationType, dfaRight); + return new DfaRelation(dfaLeft, dfaRight, relationType); } if (dfaLeft instanceof DfaConstValue && dfaRight instanceof DfaInstanceofValue) { - return createCanonicalRelation(dfaRight, relationType, dfaLeft); + return new DfaRelation(dfaRight, dfaLeft, relationType); } return null; } @@ -83,17 +83,9 @@ public final class DfaRelation extends DfaCondition { @NotNull private static DfaRelation createConstBasedRelation(DfaFactMapValue dfaLeft, RelationType relationType, DfaConstValue dfaRight) { if (dfaRight.getValue() == null && DfaNullability.isNullable(dfaLeft.getFacts())) { - return createCanonicalRelation(dfaLeft.getFactory().getFactValue(DfaFactType.NULLABILITY, DfaNullability.NULLABLE), relationType, - dfaRight); + return new DfaRelation(dfaLeft.getFactory().getFactValue(DfaFactType.NULLABILITY, DfaNullability.NULLABLE), dfaRight, relationType); } - return createCanonicalRelation(DfaUnknownValue.getInstance(), relationType, dfaRight); - } - - @NotNull - private static DfaRelation createCanonicalRelation(@NotNull final DfaValue dfaLeft, - @NotNull RelationType relationType, - @NotNull final DfaValue dfaRight) { - return new DfaRelation(dfaLeft, dfaRight, relationType); + return new DfaRelation(DfaUnknownValue.getInstance(), dfaRight, relationType); } public boolean isEquality() { diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ReorderingUtils.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ReorderingUtils.java index 5fdecb798647..90923d705e87 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ReorderingUtils.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/ReorderingUtils.java @@ -296,7 +296,7 @@ public class ReorderingUtils { ContractValue cond = conditions.get(0); DfaCondition value = cond.fromCall(myFactory, call); if (value instanceof DfaRelation) { - if (myConditions.contains(retValue == negated ? value : ((DfaRelation)value).createNegated())) { + if (myConditions.contains(retValue == negated ? value : ((DfaRelation)value).negate())) { return true; } } @@ -315,12 +315,12 @@ public class ReorderingUtils { DfaCondition value1 = leftVal.cond(relationType, rightVal); DfaCondition value2 = rightVal.cond(Objects.requireNonNull(relationType.getFlipped()), leftVal); if (value1 instanceof DfaRelation) { - if (myConditions.contains(negated ? value1 : ((DfaRelation)value1).createNegated())) { + if (myConditions.contains(negated ? value1 : ((DfaRelation)value1).negate())) { return true; } } if (value2 instanceof DfaRelation) { - if (myConditions.contains(negated ? value2 : ((DfaRelation)value2).createNegated())) { + if (myConditions.contains(negated ? value2 : ((DfaRelation)value2).negate())) { return true; } }