[java-highlighting] Display error if record patterns in for-each loops aren't available (IDEA-309351)

GitOrigin-RevId: 0919de47769682aff2c016920175a74d89c9f7b6
This commit is contained in:
Andrey Cherkasov
2022-12-24 04:37:50 +04:00
committed by intellij-monorepo-bot
parent 9e8bae7d62
commit 608c99ed4d
6 changed files with 61 additions and 8 deletions

View File

@@ -690,6 +690,12 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh
@Override
public void visitForeachStatement(@NotNull PsiForeachStatement statement) {
add(checkFeature(statement, HighlightingFeature.FOR_EACH));
if (!myHolder.hasErrorResults()) {
PsiForeachDeclarationElement iterationDeclaration = statement.getIterationDeclaration();
if (iterationDeclaration instanceof PsiPattern) {
add(checkFeature(iterationDeclaration, HighlightingFeature.RECORD_PATTERNS_IN_FOR_EACH));
}
}
}
@Override
@@ -1802,12 +1808,17 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh
public void visitVariable(@NotNull PsiVariable variable) {
super.visitVariable(variable);
if (variable instanceof PsiPatternVariable) {
PsiElement context = PsiTreeUtil.getParentOfType(variable, PsiInstanceOfExpression.class, PsiCaseLabelElementList.class);
HighlightingFeature feature = context instanceof PsiInstanceOfExpression ?
HighlightingFeature.PATTERNS :
HighlightingFeature.PATTERNS_IN_SWITCH;
PsiIdentifier varIdentifier = ((PsiPatternVariable)variable).getNameIdentifier();
add(checkFeature(varIdentifier, feature));
PsiElement context = PsiTreeUtil.getParentOfType(variable,
PsiInstanceOfExpression.class,
PsiCaseLabelElementList.class,
PsiForeachStatement.class);
if (!(context instanceof PsiForeachStatement)) {
HighlightingFeature feature = context instanceof PsiInstanceOfExpression ?
HighlightingFeature.PATTERNS :
HighlightingFeature.PATTERNS_IN_SWITCH;
PsiIdentifier varIdentifier = ((PsiPatternVariable)variable).getNameIdentifier();
add(checkFeature(varIdentifier, feature));
}
}
try {
if (!myHolder.hasErrorResults()) add(HighlightUtil.checkVarTypeApplicability(variable));

View File

@@ -48,7 +48,8 @@ public enum HighlightingFeature {
INNER_STATICS(LanguageLevel.JDK_16, "feature.inner.statics"),
PATTERNS_IN_SWITCH(LanguageLevel.JDK_17_PREVIEW, "feature.patterns.in.switch"),
GUARDED_AND_PARENTHESIZED_PATTERNS(LanguageLevel.JDK_17_PREVIEW, "feature.guarded.and.parenthesised.patterns"),
PATTERN_GUARDS_AND_RECORD_PATTERNS(LanguageLevel.JDK_19_PREVIEW, "feature.pattern.guard.and.record.patterns");
PATTERN_GUARDS_AND_RECORD_PATTERNS(LanguageLevel.JDK_19_PREVIEW, "feature.pattern.guard.and.record.patterns"),
RECORD_PATTERNS_IN_FOR_EACH(LanguageLevel.JDK_20_PREVIEW, "feature.record.patterns.in.for.each");
public static final @NonNls String JDK_INTERNAL_PREVIEW_FEATURE = "jdk.internal.PreviewFeature";
public static final @NonNls String JDK_INTERNAL_JAVAC_PREVIEW_FEATURE = "jdk.internal.javac.PreviewFeature";