[java-analysis] NullableNotNullManager: prefer type annotation over inherited annotation

Fixes IDEA-272250 False-positive warning when overridden method has a nullability type annotation

GitOrigin-RevId: a05ae5bdad95e475ac8a2af7736bcafc0e008364
This commit is contained in:
Tagir Valeev
2021-06-25 11:42:21 +00:00
committed by intellij-monorepo-bot
parent e06538a963
commit 7427f881fd
3 changed files with 29 additions and 10 deletions
@@ -258,18 +258,21 @@ public abstract class NullableNotNullManager {
memberAnno = null;
}
if (memberAnno != null) {
if (type != null) {
for (PsiAnnotation typeAnno : type.getApplicableAnnotations()) {
if (areDifferentNullityAnnotations(memberAnno.annotation, typeAnno)) {
if (typeAnno != memberAnno.annotation) return null;
Nullability nullability = annotations.getNullability(typeAnno.getQualifiedName());
if (nullability == null) return null;
return new NullabilityAnnotationInfo(typeAnno, nullability, false);
}
}
}
Nullability nullability = annotations.getNullability(memberAnno.annotation.getQualifiedName());
if (nullability == null) return null;
if (type != null) {
for (PsiAnnotation typeAnno : type.getApplicableAnnotations()) {
if (typeAnno == memberAnno.annotation) continue;
Nullability typeNullability = annotations.getNullability(typeAnno.getQualifiedName());
if (typeNullability == null) continue;
if (typeNullability != nullability) {
return null;
}
// Prefer type annotation over inherited annotation; necessary for Nullable/NotNull inspection
memberAnno = new AnnotationAndOwner(owner, typeAnno);
break;
}
}
return new NullabilityAnnotationInfo(memberAnno.annotation, nullability, memberAnno.owner == owner ? null : memberAnno.owner, false);
}
if (type instanceof PsiPrimitiveType) return null;
@@ -0,0 +1,10 @@
import typeUse.*;
abstract class A {
class B {}
@NotNull abstract A.B get();
}
abstract class C extends A {
abstract A.@NotNull B get();
}
@@ -334,6 +334,12 @@ public class NullableStuffInspectionTest extends LightJavaCodeInsightFixtureTest
doTest();
}
public void testInheritAmbiguous() {
myInspection.REPORT_ANNOTATION_NOT_PROPAGATED_TO_OVERRIDERS = true;
DataFlowInspection8Test.setupAmbiguousAnnotations("typeUse", myFixture);
doTest();
}
public void testIncorrectPlacementAmbiguous() {
DataFlowInspection8Test.setupAmbiguousAnnotations("typeUse", myFixture);
doTest();