[java] more precise highlighting for "Annotations are not allowed in deconstruction pattern types"

GitOrigin-RevId: 689808b6ab80920e12a0b5d55712df066e84a406
This commit is contained in:
Bas Leijdekkers
2024-04-01 10:32:23 +02:00
committed by intellij-monorepo-bot
parent ab2af3d314
commit 1fd9cf454d
3 changed files with 9 additions and 30 deletions

View File

@@ -1917,7 +1917,12 @@ public class HighlightVisitorImpl extends JavaElementVisitor implements Highligh
@Override
public void visitDeconstructionPattern(@NotNull PsiDeconstructionPattern deconstructionPattern) {
super.visitDeconstructionPattern(deconstructionPattern);
add(PatternHighlightingModel.checkReferenceTypeIsNotAnnotated(deconstructionPattern.getTypeElement()));
PsiTreeUtil.processElements(deconstructionPattern.getTypeElement(), PsiAnnotation.class, annotation -> {
add(HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR)
.range(annotation)
.descriptionAndTooltip(JavaErrorBundle.message("deconstruction.pattern.type.contain.annotation")));
return true;
});
PsiElement parent = deconstructionPattern.getParent();
if (parent instanceof PsiForeachPatternStatement forEach) {
add(checkFeature(deconstructionPattern, JavaFeature.RECORD_PATTERNS_IN_FOR_EACH));

View File

@@ -1,4 +1,4 @@
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.codeInsight.daemon.impl.analysis;
import com.intellij.codeInsight.daemon.JavaErrorBundle;
@@ -331,32 +331,6 @@ final class PatternHighlightingModel {
return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(pattern).descriptionAndTooltip(description);
}
private static final PsiTypeVisitor<Boolean> HAS_ANNOTATION_TYPE_VISITOR = new PsiTypeVisitor<>() {
@Override
public Boolean visitClassType(@NotNull PsiClassType classType) {
for (PsiType p : classType.getParameters()) {
if (p == null) continue;
if (p.accept(this)) return true;
}
return super.visitClassType(classType);
}
@Override
public Boolean visitType(@NotNull PsiType type) {
return type.getAnnotations().length != 0;
}
};
static HighlightInfo.Builder checkReferenceTypeIsNotAnnotated(@NotNull PsiTypeElement typeElement) {
Boolean hasAnnotation = typeElement.getType().accept(HAS_ANNOTATION_TYPE_VISITOR);
if (hasAnnotation) {
String message = JavaErrorBundle.message("deconstruction.pattern.type.contain.annotation");
return HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR)
.range(typeElement)
.descriptionAndTooltip(message);
}
return null;
}
static void checkForEachPatternApplicable(@NotNull PsiDeconstructionPattern pattern,
@NotNull PsiType patternType,
@NotNull PsiType itemType,

View File

@@ -4,11 +4,11 @@ import java.lang.annotation.Target;
public class NotAnnotationsInDeconstructionType {
private void test(T<String> t) {
t instanceof <error descr="Annotations are not allowed in deconstruction pattern types">T<@SomeAnnotation String></error>(String s);
t instanceof T<<error descr="Annotations are not allowed in deconstruction pattern types">@SomeAnnotation</error> String>(String s);
}
private void test2(T[] t) {
if (t instanceof <error descr="Annotations are not allowed in deconstruction pattern types">T @SomeAnnotation []</error>(String s)) {
if (t instanceof T <error descr="Annotations are not allowed in deconstruction pattern types">@SomeAnnotation</error> [](String s)) {
System.out.println();
}
}