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
6 changes: 5 additions & 1 deletion nullaway/src/main/java/com/uber/nullaway/NullAway.java
Original file line number Diff line number Diff line change
Expand Up @@ -738,9 +738,13 @@ public Description matchParameterizedType(ParameterizedTypeTree tree, VisitorSta
if (!withinAnnotatedCode(state)) {
return Description.NO_MATCH;
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this extra newline

Symbol calledClass = castToNonNull(ASTHelpers.getSymbol(tree));
boolean isNullUnmarked = codeAnnotationInfo.isSymbolUnannotated(calledClass, config, handler);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move this code under the if (config.isJSpecifyMode()) check below.

Also, it seems to me this logic could be simpler. If isNullUnmarked is true, can't we just skip the call to checkInstantiationForParameterizedTypedTree entirely? I don't see how that method would ever report an error if isNullUnmarked is true. This way you could avoid changing GenericsChecks

if (config.isJSpecifyMode()) {
GenericsChecks.checkInstantiationForParameterizedTypedTree(
tree, state, this, config, handler);
isNullUnmarked, tree, state, this, config, handler);
}
return Description.NO_MATCH;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ private GenericsChecks() {}
* @param handler the handler instance
*/
public static void checkInstantiationForParameterizedTypedTree(
boolean isNullUnmarked,
ParameterizedTypeTree tree,
VisitorState state,
NullAway analysis,
Expand Down Expand Up @@ -105,7 +106,8 @@ public static void checkInstantiationForParameterizedTypedTree(
upperBound.getAnnotationMirrors();
boolean hasNullableAnnotation =
Nullness.hasNullableAnnotation(annotationMirrors.stream(), config)
|| handler.onOverrideTypeParameterUpperBound(baseType.tsym.toString(), i);
|| handler.onOverrideTypeParameterUpperBound(baseType.tsym.toString(), i)
|| isNullUnmarked;
// if base type argument does not have @Nullable annotation then the instantiation is
// invalid
if (!hasNullableAnnotation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1737,6 +1737,33 @@ 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<S> {",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Java, class names should start with a capital letter: https://google.github.io/styleguide/javaguide.html#s5.2.2-class-names

" static <T> void m1(T t) {}",
" }",
" @NullMarked",
" static class markedClass {",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again class name should start with capital letter

" static void testInstantiation() {",
" new nullUnmarkedClass<@Nullable String>();",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a comment here (within quotes) indicating that despite the type variable not having a @Nullable upper bound, we don't expect an error since the type is @NullUnmarked

" }",
" static void testAssignment() {",
" nullUnmarkedClass<@Nullable Integer> var = null;",
" }",
" }",
"}")
.doTest();
}

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