Skip to content

Commit

Permalink
[java-analysis] Parameter nullability: prefer nullability known from …
Browse files Browse the repository at this point in the history
…type over nullability known from parameter declaration

Type nullability could be more precise if parameter is generic
Fixes IDEA-364343 False-positive NPE at unboxing inside lambda with JSpecify annotations

GitOrigin-RevId: 9a49f5687eccaa013e639cdf15950be911e100bc
  • Loading branch information
amaembo authored and intellij-monorepo-bot committed Dec 6, 2024
1 parent 74904a8 commit ce18179
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -307,12 +307,12 @@ public static Nullability getFunctionalParameterNullability(PsiFunctionalExpress
if (sam != null) {
PsiParameter parameter = sam.getParameterList().getParameter(index);
if (parameter != null) {
Nullability nullability = getElementNullability(null, parameter);
if (nullability != Nullability.UNKNOWN) {
return nullability;
}
PsiType parameterType = type.resolveGenerics().getSubstitutor().substitute(parameter.getType());
return getTypeNullability(GenericsUtil.eliminateWildcards(parameterType, false, true));
NullabilityAnnotationInfo info = getTypeNullabilityInfo(GenericsUtil.eliminateWildcards(parameterType, false, true));
if (info != null) {
return info.getNullability();
}
return getElementNullability(null, parameter);
}
}
return Nullability.UNKNOWN;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

// IDEA-364343
@NullMarked
class AnotherActivity {
public interface ThrowingFunction<T1 extends @Nullable Object, T2 extends @Nullable Object> {
T2 apply(T1 input) throws Throwable;
}

abstract static class Decoder<T extends @Nullable Object> {
abstract <T2 extends @Nullable Object> Decoder<T2> then(
ThrowingFunction<? super T, ? extends T2> dataTransform);
}

native Decoder<Boolean> foo();

Decoder<Boolean> doWork() {
return foo().then(f -> !f);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,10 @@ public void testJSpecifyUpperBound() {
setupTypeUseAnnotations("org.jspecify.annotations", myFixture);
doTest();
}

public void testJSpecifyUnboxingInLambda() {
addJSpecifyNullMarked(myFixture);
setupTypeUseAnnotations("org.jspecify.annotations", myFixture);
doTest();
}
}

0 comments on commit ce18179

Please sign in to comment.