diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/DataFlowInspectionBase.java b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/DataFlowInspectionBase.java index bca5390a66ee..6b12851cb9a2 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/DataFlowInspectionBase.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/DataFlowInspectionBase.java @@ -196,7 +196,7 @@ public class DataFlowInspectionBase extends BaseJavaBatchLocalInspectionTool { ProblemsHolder holder, StandardDataFlowRunner dfaRunner, Collection initialStates, final boolean onTheFly) { - final DataFlowInstructionVisitor visitor = new DataFlowInstructionVisitor(dfaRunner); + final DataFlowInstructionVisitor visitor = new DataFlowInstructionVisitor(); final RunnerResult rc = dfaRunner.analyzeMethod(scope, visitor, IGNORE_ASSERT_STATEMENTS, initialStates); if (rc == RunnerResult.OK) { createDescription(dfaRunner, holder, visitor, onTheFly, scope); @@ -257,7 +257,7 @@ public class DataFlowInspectionBase extends BaseJavaBatchLocalInspectionTool { protected void addSurroundWithIfFix(PsiExpression qualifier, List fixes, boolean onTheFly) { } - private void createDescription(StandardDataFlowRunner runner, ProblemsHolder holder, DataFlowInstructionVisitor visitor, final boolean onTheFly, PsiElement scope) { + private void createDescription(StandardDataFlowRunner runner, ProblemsHolder holder, final DataFlowInstructionVisitor visitor, final boolean onTheFly, PsiElement scope) { Pair, Set> constConditions = runner.getConstConditionalExpressions(); Set trueSet = constConditions.getFirst(); Set falseSet = constConditions.getSecond(); @@ -265,8 +265,13 @@ public class DataFlowInspectionBase extends BaseJavaBatchLocalInspectionTool { ArrayList allProblems = new ArrayList(); allProblems.addAll(trueSet); allProblems.addAll(falseSet); - allProblems.addAll(runner.getCCEInstructions()); - allProblems.addAll(StandardDataFlowRunner.getRedundantInstanceofs(runner, visitor)); + allProblems.addAll(visitor.myCCEInstructions); + allProblems.addAll(ContainerUtil.filter(runner.getInstructions(), new Condition() { + @Override + public boolean value(Instruction instruction1) { + return instruction1 instanceof InstanceofInstruction && visitor.isInstanceofRedundant((InstanceofInstruction)instruction1); + } + })); HashSet reportedAnchors = new HashSet(); for (PsiElement element : visitor.getProblems(NullabilityProblem.callNPE)) { @@ -795,17 +800,13 @@ public class DataFlowInspectionBase extends BaseJavaBatchLocalInspectionTool { } private static class DataFlowInstructionVisitor extends StandardInstructionVisitor { - private final StandardDataFlowRunner myRunner; private final MultiMap myProblems = new MultiMap(); private final Map, StateInfo> myStateInfos = ContainerUtil.newHashMap(); - - private DataFlowInstructionVisitor(StandardDataFlowRunner runner) { - myRunner = runner; - } + private final Set myCCEInstructions = ContainerUtil.newHashSet(); @Override protected void onInstructionProducesCCE(TypeCastInstruction instruction) { - myRunner.onInstructionProducesCCE(instruction); + myCCEInstructions.add(instruction); } Collection getProblems(final NullabilityProblem kind) { diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/StandardDataFlowRunner.java b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/StandardDataFlowRunner.java index ed0cc5ad0424..984858ebed50 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/StandardDataFlowRunner.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/StandardDataFlowRunner.java @@ -24,15 +24,7 @@ */ package com.intellij.codeInspection.dataFlow; -import com.intellij.codeInspection.dataFlow.instructions.InstanceofInstruction; -import com.intellij.codeInspection.dataFlow.instructions.Instruction; -import org.jetbrains.annotations.NotNull; - -import java.util.HashSet; -import java.util.Set; - public class StandardDataFlowRunner extends DataFlowRunner { - private final Set myCCEInstructions = new HashSet(); public StandardDataFlowRunner() { this(false, true); @@ -40,23 +32,4 @@ public class StandardDataFlowRunner extends DataFlowRunner { public StandardDataFlowRunner(boolean unknownMembersAreNullable, boolean honorFieldInitializers) { super(unknownMembersAreNullable, honorFieldInitializers); } - - public void onInstructionProducesCCE(Instruction instruction) { - myCCEInstructions.add(instruction); - } - - @NotNull public Set getCCEInstructions() { - return myCCEInstructions; - } - - @NotNull public static Set getRedundantInstanceofs(final DataFlowRunner runner, StandardInstructionVisitor visitor) { - HashSet result = new HashSet(1); - for (Instruction instruction : runner.getInstructions()) { - if (instruction instanceof InstanceofInstruction && visitor.isInstanceofRedundant((InstanceofInstruction)instruction)) { - result.add(instruction); - } - } - - return result; - } } diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/DoubleCCEWarning.java b/java/java-tests/testData/inspection/dataFlow/fixture/DoubleCCEWarning.java new file mode 100644 index 000000000000..047c2de64f29 --- /dev/null +++ b/java/java-tests/testData/inspection/dataFlow/fixture/DoubleCCEWarning.java @@ -0,0 +1,21 @@ +class X { + public static void main(Object result) { + final boolean varargs = result instanceof Intf; + new Runnable() { + public void run() { } + }; + Intf a = (Intf)result; + } + + public static void main2(Object result) { + final boolean varargs = result instanceof Intf; + if (!varargs) return; + new Runnable() { + public void run() { } + }; + if (result instanceof Intf) { + System.out.println(); + } + } +} +interface Intf {} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/codeInspection/DataFlowInspectionTest.java b/java/java-tests/testSrc/com/intellij/codeInspection/DataFlowInspectionTest.java index e80e90aed0fc..1ec4ca567e99 100644 --- a/java/java-tests/testSrc/com/intellij/codeInspection/DataFlowInspectionTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInspection/DataFlowInspectionTest.java @@ -241,6 +241,7 @@ public class DataFlowInspectionTest extends LightCodeInsightFixtureTestCase { public void testBoxingImpliesNotNull() { doTest(); } public void testLargeIntegersAreNotEqualWhenBoxed() { doTest(); } public void testNoGenericCCE() { doTest(); } + public void testDoubleCCEWarning() { doTest(); } public void testLongCircuitOperations() { doTest(); } public void testUnconditionalForLoop() { doTest(); } public void testAnonymousMethodIndependence() { doTest(); }