[java-analysis] IDEA-375799 Container nullability annotation on the method has no effect on method return type

(cherry picked from commit f7cead47f32b2befeabe116a6c2df87682cb1cd2)

IJ-CR-169010

GitOrigin-RevId: 036988a6259830532f2174c60c39996d71efe819
This commit is contained in:
Tagir Valeev
2025-07-11 12:19:15 +02:00
committed by intellij-monorepo-bot
parent 830a094ed8
commit a0fec72e48
4 changed files with 29 additions and 2 deletions

View File

@@ -350,7 +350,7 @@ public abstract class NullableNotNullManager {
private @Nullable NullabilityAnnotationInfo findNullabilityDefault(@NotNull PsiElement place,
@NotNull PsiAnnotation.TargetType @NotNull ... placeTargetTypes) {
PsiElement element = place.getContext();
PsiElement element = place;
while (element != null) {
if (element instanceof PsiModifierListOwner) {
NullabilityAnnotationInfo result = getNullityDefault((PsiModifierListOwner)element, placeTargetTypes).forContext(place);

View File

@@ -0,0 +1,14 @@
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.NullUnmarked;
import org.jspecify.annotations.Nullable;
import java.util.List;
@NullMarked
class Container<T extends @Nullable Object> {
@NullUnmarked
List<T> get() {
return <warning descr="'null' is returned by the method which is not declared as @Nullable">null</warning>;
}
}

View File

@@ -221,5 +221,11 @@ public class DataFlowInspection21Test extends DataFlowInspectionTestCase {
addJSpecifyNullMarked(myFixture);
doTest();
}
public void testJSpecifyNullUnmarkedOverNullMarked() {
addJSpecifyNullMarked(myFixture);
setupTypeUseAnnotations("org.jspecify.annotations", myFixture);
doTest();
}
}

View File

@@ -63,9 +63,16 @@ public abstract class DataFlowInspectionTestCase extends LightJavaCodeInsightFix
"""
package org.jspecify.annotations;
import java.lang.annotation.*;
@Target({ElementType.TYPE, ElementType.MODULE})
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.MODULE})
public @interface NullMarked {}""";
fixture.addClass(nullMarked);
@Language("JAVA") String nullUnmarked =
"""
package org.jspecify.annotations;
import java.lang.annotation.*;
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.MODULE})
public @interface NullUnmarked {}""";
fixture.addClass(nullUnmarked);
}
public static void setupTypeUseAnnotations(String pkg, JavaCodeInsightTestFixture fixture) {