Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't report @Nullable type argument errors for unmarked classes #958

Merged
merged 11 commits into from
Jun 15, 2024
9 changes: 7 additions & 2 deletions nullaway/src/main/java/com/uber/nullaway/NullAway.java
Original file line number Diff line number Diff line change
Expand Up @@ -712,8 +712,13 @@ public Description matchParameterizedType(ParameterizedTypeTree tree, VisitorSta
return Description.NO_MATCH;
}
if (config.isJSpecifyMode()) {
GenericsChecks.checkInstantiationForParameterizedTypedTree(
tree, state, this, config, handler);
Symbol baseClass = ASTHelpers.getSymbol(tree);
boolean isNullUnmarked =
baseClass != null && codeAnnotationInfo.isSymbolUnannotated(baseClass, config, handler);
if (!isNullUnmarked) {
GenericsChecks.checkInstantiationForParameterizedTypedTree(
tree, state, this, config, handler);
}
}
return Description.NO_MATCH;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1737,6 +1737,34 @@ public void testRawTypeReceiverCast() {
.doTest();
}

@Test
public void testUseOfUnannotatedCode() {
makeHelper()
.addSourceLines(
"Test.java",
"package com.uber;",
"import org.jspecify.annotations.NullMarked;",
"import org.jspecify.annotations.NullUnmarked;",
"import org.jspecify.annotations.Nullable;",
"class Test {",
" @NullUnmarked",
" static class NullUnmarkedClass<T> {",
" }",
" @NullMarked",
" static class MarkedClass {",
" static void testInstantiation() {",
" // NullUnmarkedClass is marked @NullUnmarked, so we get no error",
" // even though the type variable does not have a @Nullable upper bound",
" new NullUnmarkedClass<@Nullable String>();",
" }",
" static void testAssignment() {",
" NullUnmarkedClass<@Nullable Integer> var = null;",
" }",
" }",
"}")
.doTest();
}

public void boxInteger() {
makeHelper()
.addSourceLines(
Expand Down