ControlFlowUtils#isReachable: switch expression handling fixed

Fixes IDEA-192129 Logic is changed when an 'if' statement is simplified
This commit is contained in:
Tagir Valeev
2018-05-17 15:55:49 +07:00
parent 88a3ab8a3e
commit f889737be8
4 changed files with 42 additions and 2 deletions
@@ -993,8 +993,12 @@ public class ControlFlowUtils {
*/
public static boolean isReachable(@NotNull PsiStatement statement) {
ControlFlow flow;
PsiCodeBlock block = PsiTreeUtil.getParentOfType(statement, PsiCodeBlock.class);
if (block == null) return true;
PsiElement block = statement;
do {
block = PsiTreeUtil.getParentOfType(block, PsiCodeBlock.class);
if (block == null) return true;
}
while (block.getParent() instanceof PsiSwitchStatement);
try {
flow = ControlFlowFactory.getInstance(statement.getProject())
.getControlFlow(block, LocalsOrMyInstanceFieldsControlFlowPolicy.getInstance());
@@ -0,0 +1,17 @@
// Copyright 2000-2017 JetBrains s.r.o.
// Use of this source code is governed by the Apache 2.0 license that can be
// found in the LICENSE file.
class InCodeBlock {
private static boolean original(boolean first, boolean second, int i) {
switch (i) {
case 0:
return false;
case 1:
if (first)
return second;
return false;
default:
return true;
}
}
}
@@ -0,0 +1,18 @@
// Copyright 2000-2017 JetBrains s.r.o.
// Use of this source code is governed by the Apache 2.0 license that can be
// found in the LICENSE file.
class InCodeBlock {
private static boolean original(boolean first, boolean second, int i) {
switch (i) {
case 0:
return false;
case 1:
if (first)
i<caret>f (second)
return true;
return false;
default:
return true;
}
}
}
@@ -37,4 +37,5 @@ public class TrivialIfFixTest extends IGQuickFixesTestCase {
public void testParentheses() { doTest(); }
public void testNested() { doTest(); }
public void testInCodeBlock() { doTest(); }
public void testInSwitch() { doTest(); }
}