can complete normally: break inside switch (IDEA-178304)

break inside switch when switch is selected should not lead to abnormal completion
This commit is contained in:
Anna Kozlova
2017-09-04 11:59:25 +03:00
parent 0fb58a95a1
commit b23c6b026b
4 changed files with 26 additions and 3 deletions

View File

@@ -885,7 +885,7 @@ class ControlFlowAnalyzer extends JavaElementVisitor {
if (((PsiSwitchLabelStatement)aStatement).isDefaultCase()) {
defaultLabel = (PsiSwitchLabelStatement)aStatement;
}
Instruction instruction = new ConditionalGoToInstruction(0, statement.getExpression());
Instruction instruction = new ConditionalGoToInstruction(0, expr);
myCurrentFlow.addInstruction(instruction);
addElementOffsetLater(aStatement, true);
}

View File

@@ -1044,8 +1044,17 @@ public class ControlFlowUtil {
boolean isNormal = nextOffset <= endOffset && !isReturn && (nextOffset == endOffset || canCompleteNormally[nextOffset]);
if (isNormal && nextOffset == endOffset) {
PsiElement element = flow.getElement(offset);
if (element instanceof PsiBreakStatement || element instanceof PsiContinueStatement) {
isNormal = false;
if (element instanceof PsiBreakStatement) {
PsiStatement exitedStatement = ((PsiBreakStatement)element).findExitedStatement();
if (exitedStatement == null || flow.getStartOffset(exitedStatement) < startOffset) {
isNormal = false;
}
}
else if (element instanceof PsiContinueStatement) {
PsiStatement continuedStatement = ((PsiContinueStatement)element).findContinuedStatement();
if (continuedStatement == null || flow.getStartOffset(continuedStatement) < startOffset) {
isNormal = false;
}
}
}
canCompleteNormally[offset] |= isNormal;

View File

@@ -0,0 +1,13 @@
import java.util.concurrent.ExecutorService;
class Test {
private void m(ExecutorService service, int i) {
service.submit(() -> {
switch (i) {
default:
break;
}
});
}
}

View File

@@ -161,6 +161,7 @@ public class NewLambdaHighlightingTest extends LightDaemonAnalyzerTestCase {
public void testConditionalExpressionInLambdaReturns() { doTest(); }
public void testLambdaWithFormalTypeParameters() { doTest(); }
public void testIDEA174924() { doTest(); }
public void testVoidValueCompatibilityWithBreakInSwitch() { doTest(); }
private void doTest() {
IdeaTestUtil.setTestVersion(JavaSdkVersion.JDK_1_8, getModule(), getTestRootDisposable());