mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-199693 Unreachable switch statements are not highlighted after always reachable branch
This commit is contained in:
+12
-1
@@ -43,7 +43,6 @@ import java.util.*;
|
||||
|
||||
import static com.intellij.util.ObjectUtils.tryCast;
|
||||
|
||||
@SuppressWarnings("ConditionalExpressionWithIdenticalBranches")
|
||||
public class DataFlowInspectionBase extends AbstractBaseJavaLocalInspectionTool {
|
||||
static final Logger LOG = Logger.getInstance("#com.intellij.codeInspection.dataFlow.DataFlowInspection");
|
||||
@NonNls private static final String SHORT_NAME = "ConstantConditions";
|
||||
@@ -692,6 +691,18 @@ public class DataFlowInspectionBase extends AbstractBaseJavaLocalInspectionTool
|
||||
holder.registerProblem(psiAnchor,
|
||||
InspectionsBundle.message("dataflow.message.unreachable.switch.label"),
|
||||
new DeleteSwitchLabelFix((PsiSwitchLabelStatement)psiAnchor));
|
||||
} else if (trueSet.contains(instruction)) {
|
||||
// If switch branch is always reachable, then all the subsequent branches are unreachable (thus weren't analyzed)
|
||||
PsiSwitchLabelStatement current = (PsiSwitchLabelStatement)psiAnchor;
|
||||
while(true) {
|
||||
current = PsiTreeUtil.getNextSiblingOfType(current, PsiSwitchLabelStatement.class);
|
||||
if (current == null) break;
|
||||
if (!current.isDefaultCase()) {
|
||||
holder.registerProblem(current,
|
||||
InspectionsBundle.message("dataflow.message.unreachable.switch.label"),
|
||||
new DeleteSwitchLabelFix((PsiSwitchLabelStatement)psiAnchor));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (psiAnchor != null && !isFlagCheck(psiAnchor)) {
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
class Scratch {
|
||||
public static void main(String[] args) {
|
||||
switch("ping") {
|
||||
case "ping":
|
||||
System.out.println("ping");
|
||||
break;
|
||||
<warning descr="Switch label 'case \"pong\":' is unreachable">case "pong":</warning>
|
||||
System.out.println("pong");
|
||||
break;
|
||||
<warning descr="Switch label 'case \"simple\":' is unreachable">case "simple":</warning>
|
||||
System.out.println("simple");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
switch("ping") {
|
||||
<warning descr="Switch label 'case \"pong\":' is unreachable">case "pong":</warning>
|
||||
System.out.println("pong");
|
||||
break;
|
||||
case "ping":
|
||||
System.out.println("ping");
|
||||
break;
|
||||
<warning descr="Switch label 'case \"simple\":' is unreachable">case "simple":</warning>
|
||||
System.out.println("simple");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
switch("ping") {
|
||||
<warning descr="Switch label 'case \"pong\":' is unreachable">case "pong":</warning>
|
||||
System.out.println("pong");
|
||||
break;
|
||||
<warning descr="Switch label 'case \"simple\":' is unreachable">case "simple":</warning>
|
||||
System.out.println("simple");
|
||||
break;
|
||||
case "ping":
|
||||
System.out.println("ping");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -657,4 +657,5 @@ public class DataFlowInspectionTest extends DataFlowInspectionTestCase {
|
||||
assertEquals(1, intentions.stream().filter(i -> i.getText().equals("Remove 'if' statement")).count());
|
||||
assertEquals(0, intentions.stream().filter(i -> i.getText().equals("Simplify 'expirationDay != other.expirationDay' to false")).count());
|
||||
}
|
||||
public void testAlwaysTrueSwitchLabel() { doTest(); }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user