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
10 changes: 8 additions & 2 deletions nullaway/src/main/java/com/uber/nullaway/NullAway.java
Original file line number Diff line number Diff line change
Expand Up @@ -738,10 +738,16 @@ 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

if (config.isJSpecifyMode()) {
GenericsChecks.checkInstantiationForParameterizedTypedTree(
tree, state, this, config, handler);
Symbol calledClass = ASTHelpers.getSymbol(tree);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think baseClass is a better name for this variable. "called" implies something like a method call.

boolean isNullUnmarked = codeAnnotationInfo.isSymbolUnannotated(calledClass, config, handler);
if (!isNullUnmarked) {
GenericsChecks.checkInstantiationForParameterizedTypedTree(
tree, state, this, 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.

also remove this newline

return Description.NO_MATCH;
}

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