switch expression: missed error on continue without lbl outside of switch (IDEA-204208)

This commit is contained in:
Anna.Kozlova
2018-12-13 18:01:51 +01:00
parent 67422555d5
commit b4d7bd2bcf
3 changed files with 12 additions and 4 deletions
@@ -805,13 +805,13 @@ public class HighlightUtil extends HighlightUtilBase {
}
@Nullable
static HighlightInfo checkContinueOutsideLoop(@NotNull PsiContinueStatement statement) {
static HighlightInfo checkContinueOutsideLoop(@NotNull PsiContinueStatement statement, LanguageLevel languageLevel) {
if (PsiImplUtil.findEnclosingLoop(statement) == null) {
String message = JavaErrorMessages.message("continue.outside.loop");
return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(statement).descriptionAndTooltip(message).create();
}
return null;
return checkContinueOutsideOfSwitchExpression(statement,statement.findContinuedStatement(), languageLevel);
}
@Nullable
@@ -827,12 +827,19 @@ public class HighlightUtil extends HighlightUtilBase {
return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(statement).descriptionAndTooltip(message).create();
}
return checkContinueOutsideOfSwitchExpression(statement, continuedStatement, level);
}
private static HighlightInfo checkContinueOutsideOfSwitchExpression(@NotNull PsiContinueStatement statement,
PsiStatement continuedStatement,
@NotNull LanguageLevel level) {
if (level.isAtLeast(LanguageLevel.JDK_12_PREVIEW)) {
PsiElement enclosing = PsiImplUtil.findEnclosingSwitchOrLoop(statement);
if (enclosing instanceof PsiSwitchExpression && PsiTreeUtil.isAncestor(continuedStatement, enclosing, true)) {
String message = JavaErrorMessages.message("continue.outside.switch.expr");
return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(statement).descriptionAndTooltip(message).create();
}
return null;
}
return null;
@@ -454,7 +454,7 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh
super.visitContinueStatement(statement);
if (!myHolder.hasErrorResults()) {
PsiIdentifier label = statement.getLabelIdentifier();
myHolder.add(label == null ? HighlightUtil.checkContinueOutsideLoop(statement)
myHolder.add(label == null ? HighlightUtil.checkContinueOutsideLoop(statement, myLanguageLevel)
: HighlightUtil.checkContinueTarget(statement, label, myLanguageLevel));
}
}
@@ -54,6 +54,7 @@ class SwitchExpressions {
System.out.println(switch (new Random().nextInt()) {
case -1: <error descr="Return outside of enclosing switch expression">return;</error>
case -2: <error descr="Continue outside of enclosing switch expression">continue lab;</error>
case -3: <error descr="Continue outside of enclosing switch expression">continue;</error>
default: <error descr="Break outside of enclosing switch expression">break lab;</error>
});
}