Skip to content

Commit 90265c5

Browse files
authored
Skip checks involving wildcard generic type arguments (#1137)
We need to handle wildcards eventually, but in the meantime, avoid reporting false positives. Fixes #1126
1 parent 427fa89 commit 90265c5

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

nullaway/src/main/java/com/uber/nullaway/generics/CheckIdenticalNullabilityVisitor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ public Boolean visitClassType(Type.ClassType lhsType, Type rhsType) {
5555
for (int i = 0; i < lhsTypeArguments.size(); i++) {
5656
Type lhsTypeArgument = lhsTypeArguments.get(i);
5757
Type rhsTypeArgument = rhsTypeArguments.get(i);
58+
if (lhsTypeArgument.getKind().equals(TypeKind.WILDCARD)
59+
|| rhsTypeArgument.getKind().equals(TypeKind.WILDCARD)) {
60+
// TODO Handle wildcard types
61+
continue;
62+
}
5863
boolean isLHSNullableAnnotated = GenericsChecks.isNullableAnnotated(lhsTypeArgument, state);
5964
boolean isRHSNullableAnnotated = GenericsChecks.isNullableAnnotated(rhsTypeArgument, state);
6065
if (isLHSNullableAnnotated != isRHSNullableAnnotated) {

nullaway/src/test/java/com/uber/nullaway/jspecify/GenericsTests.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2069,6 +2069,42 @@ public void nullUnmarkedGenericField() {
20692069
.doTest();
20702070
}
20712071

2072+
@Test
2073+
public void issue1126() {
2074+
makeHelper()
2075+
.addSourceLines(
2076+
"Test.java",
2077+
"package com.uber;",
2078+
"import org.jspecify.annotations.Nullable;",
2079+
"import java.util.function.Supplier;",
2080+
"public class Test {",
2081+
" static class K<T extends @Nullable Object> {}",
2082+
" void foo(K<@Nullable Object> k) {",
2083+
" K<? extends @Nullable Object> k2 = k;",
2084+
" Supplier<? extends @Nullable Object> s = () -> null;",
2085+
" }",
2086+
"}")
2087+
.addSourceLines(
2088+
"Test2.java",
2089+
"package com.uber;",
2090+
"import java.util.HashMap;",
2091+
"import java.util.Map;",
2092+
"import org.jspecify.annotations.Nullable;",
2093+
"import org.jetbrains.annotations.Contract;",
2094+
"public class Test2 {",
2095+
" @Contract(\"null -> true\")",
2096+
" public static boolean isEmpty(@Nullable Map<?, ? extends @Nullable Object> map) {",
2097+
" return (map == null || map.isEmpty());",
2098+
" }",
2099+
" static void foo() {",
2100+
" Map<String, @Nullable Object> variables = new HashMap<>();",
2101+
" if (isEmpty(variables)) { /* do nothing */ }",
2102+
" variables.toString();",
2103+
" }",
2104+
"}")
2105+
.doTest();
2106+
}
2107+
20722108
private CompilationTestHelper makeHelper() {
20732109
return makeTestHelperWithArgs(
20742110
Arrays.asList(

0 commit comments

Comments
 (0)