[java-inspections] IDEA-377764 Incorrect usages of annotations. Annotations can be used for 'throws' lists

GitOrigin-RevId: e2d03f73bd84865de29e48d21c492620497e13f3
This commit is contained in:
Tagir Valeev
2025-09-29 09:18:43 +00:00
committed by intellij-monorepo-bot
parent 7a02b75e30
commit d5653fd75b
7 changed files with 15 additions and 11 deletions
@@ -254,6 +254,7 @@ inspection.nullable.problems.at.wildcard=Nullability annotation is not applicabl
inspection.nullable.problems.at.local.variable=Nullability annotation is not applicable to local variables
inspection.nullable.problems.at.type.parameter=Nullability annotation is not applicable to type parameters
inspection.nullable.problems.at.reference.list=Nullability annotation is not applicable to extends/implements clause
inspection.nullable.problems.at.throws=Nullability annotation is not applicable to 'throws' clause
inspection.nullable.problems.constructor.not.compatible.non.null.type.argument=Constructor is not compatible with a non-null type argument
inspection.objects.equals.can.be.simplified.display.name='Objects.equals()' can be replaced with 'equals()'
inspection.redundant.cast.problem.descriptor=Casting <code>{0}</code> to <code>#ref</code> #loc is redundant
@@ -245,8 +245,7 @@ public class NullableStuffInspectionBase extends AbstractBaseJavaLocalInspection
PsiAnnotationOwner owner = annotation.getOwner();
PsiModifierListOwner listOwner = owner instanceof PsiModifierList modifierList
? tryCast(modifierList.getParent(), PsiModifierListOwner.class) : null;
PsiType targetType = listOwner instanceof PsiMethod method ? method.getReturnType() :
listOwner instanceof PsiVariable variable ? variable.getType() : null;
PsiType targetType = listOwner == null ? null : PsiUtil.getTypeByPsiElement(listOwner);
if (listOwner != null && targetType != null) {
checkRedundantInContainerScope(annotation, manager.findContainerAnnotation(listOwner), nullability);
}
@@ -298,6 +297,9 @@ public class NullableStuffInspectionBase extends AbstractBaseJavaLocalInspection
!(parent.getParent() instanceof PsiTypeParameter)) {
reportIncorrectLocation(holder, annotation, listOwner, "inspection.nullable.problems.at.reference.list");
}
if (PsiUtil.isJavaToken(firstChild, JavaTokenType.THROWS_KEYWORD)) {
reportIncorrectLocation(holder, annotation, listOwner, "inspection.nullable.problems.at.throws");
}
}
}
}
@@ -104,7 +104,7 @@ public class Other {
void throwsN() throws @Nullable Exception;
// test:name:NonNull exception type
// test:irrelevant-annotation:NonNulll
// test:irrelevant-annotation:NonNull
void throwsR() throws @NonNull Exception;
}
}
@@ -104,7 +104,7 @@ public class Other {
void throwsN() throws @Nullable Exception;
// test:name:NonNull exception type
// test:irrelevant-annotation:NonNulll
// test:irrelevant-annotation:NonNull
void throwsR() throws @NonNull Exception;
}
}
@@ -106,7 +106,7 @@ public class Other {
void throwsN() throws @Nullable Exception;
// test:name:NonNull exception type
// test:irrelevant-annotation:NonNulll
// test:irrelevant-annotation:NonNull
void throwsR() throws @NonNull Exception;
}
}
@@ -2,6 +2,11 @@ import typeUse.*;
public class IncorrectPlacement extends <warning descr="Nullability annotation is not applicable to extends/implements clause">@Nullable</warning> Object
implements <warning descr="Nullability annotation is not applicable to extends/implements clause">@Nullable</warning> Cloneable {
interface X {
void test() throws <warning descr="Nullability annotation is not applicable to 'throws' clause">@Nullable</warning> Exception;
void test2() throws <warning descr="Nullability annotation is not applicable to 'throws' clause">@NotNull</warning> Exception;
}
<warning descr="Nullability annotation is not applicable to constructors">@Nullable</warning> IncorrectPlacement() {}
@@ -96,12 +96,7 @@ public class JSpecifyConformanceAnnotationTest extends LightJavaCodeInsightFixtu
Pair.create("Irrelevant.java", 46), // see: IDEA-377761
Pair.create("Other.java", 72), // see: IDEA-377763
Pair.create("Other.java", 70), // see: IDEA-377763
Pair.create("Other.java", 102), // see: IDEA-377764
Pair.create("Other.java", 106), // see: IDEA-377764
Pair.create("Other.java", 104), // see: IDEA-377764
Pair.create("Other.java", 108) // see: IDEA-377764
Pair.create("Other.java", 70) // see: IDEA-377763
);
LineColumn column = StringUtil.offsetToLineColumn(message, offset);
return suppressed.contains(Pair.create(fileName, column.line));
@@ -232,6 +227,7 @@ public class JSpecifyConformanceAnnotationTest extends LightJavaCodeInsightFixtu
@NotNull String messageKey, Object... args) {
switch (messageKey) {
case "inspection.nullable.problems.primitive.type.annotation",
"inspection.nullable.problems.at.throws",
"inspection.nullable.problems.at.type.parameter",
"inspection.nullable.problems.Nullable.NotNull.conflict",
"inspection.nullable.problems.at.wildcard",