From 3aa97e08fa78f7b742dded8368f40d40efd0dd4a Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Thu, 29 Apr 2021 10:59:43 +0700 Subject: [PATCH] [java-dfa] Fix false-positive at array initialization GitOrigin-RevId: b36b1373c6f7691af6dd129d80bf250a3d833727 --- .../codeInspection/dataFlow/java/ControlFlowAnalyzer.java | 5 +---- .../dataFlow/fixture/InitArrayInConstructor.java | 7 +++++++ .../java/codeInspection/DataFlowInspectionTest.java | 1 + 3 files changed, 9 insertions(+), 4 deletions(-) create mode 100644 java/java-tests/testData/inspection/dataFlow/fixture/InitArrayInConstructor.java diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/java/ControlFlowAnalyzer.java b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/java/ControlFlowAnalyzer.java index 02b3258e8933..fd6094c6572b 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/java/ControlFlowAnalyzer.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/java/ControlFlowAnalyzer.java @@ -1841,10 +1841,7 @@ public class ControlFlowAnalyzer extends JavaElementVisitor { if (val instanceof Integer) { int lengthValue = (Integer)val; if (lengthValue > 0 && lengthValue <= MAX_UNROLL_SIZE) { - DfaVariableValue var = getTargetVariable(arrayExpression); - if (var == null) { - var = createTempVariable(type); - } + DfaVariableValue var = createTempVariable(type); addInstruction(new PushInstruction(var, null, true)); addInstruction(new SwapInstruction()); addInstruction(new AssignInstruction(null, var)); diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/InitArrayInConstructor.java b/java/java-tests/testData/inspection/dataFlow/fixture/InitArrayInConstructor.java new file mode 100644 index 000000000000..24fc2c0ff2b5 --- /dev/null +++ b/java/java-tests/testData/inspection/dataFlow/fixture/InitArrayInConstructor.java @@ -0,0 +1,7 @@ +public class InitArrayInConstructor { + double[] arr; + + InitArrayInConstructor() { + arr = new double[3]; + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspectionTest.java b/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspectionTest.java index bacea9967df5..0da93e962496 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspectionTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspectionTest.java @@ -698,4 +698,5 @@ public class DataFlowInspectionTest extends DataFlowInspectionTestCase { public void testBoxedDivisionComparison() { doTest(); } public void testUnknownComparedToNullable() { doTest(); } public void testCastInCatch() { doTest(); } + public void testInitArrayInConstructor() { doTest(); } }