Skip to content

Commit

Permalink
Don't report initializer warnings on @NullUnmarked constructors / met…
Browse files Browse the repository at this point in the history
…hods (#997)

Fixes #995
  • Loading branch information
msridhar authored Jul 16, 2024
1 parent 40576b7 commit ac72b68
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
5 changes: 4 additions & 1 deletion nullaway/src/main/java/com/uber/nullaway/ErrorBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

package com.uber.nullaway;

import static com.uber.nullaway.ASTHelpersBackports.hasDirectAnnotationWithSimpleName;
import static com.uber.nullaway.ASTHelpersBackports.isStatic;
import static com.uber.nullaway.ErrorMessage.MessageTypes.FIELD_NO_INIT;
import static com.uber.nullaway.ErrorMessage.MessageTypes.GET_ON_EMPTY_OPTIONAL;
Expand Down Expand Up @@ -415,7 +416,9 @@ void reportInitializerError(
// Check needed here, despite check in hasPathSuppression because initialization
// checking happens at the class-level (meaning state.getPath() might not include the
// method itself).
if (symbolHasSuppressWarningsAnnotation(methodSymbol, INITIALIZATION_CHECK_NAME)) {
if (symbolHasSuppressWarningsAnnotation(methodSymbol, INITIALIZATION_CHECK_NAME)
|| hasDirectAnnotationWithSimpleName(
methodSymbol, NullabilityUtil.NULLUNMARKED_SIMPLE_NAME)) {
return;
}
Tree methodTree = getTreesInstance(state).getTree(methodSymbol);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1156,4 +1156,21 @@ public void dotClassSanityTest2() {
"}")
.doTest();
}

@Test
public void nullUnmarkedOnConstructorSuppressesInitializerWarnings() {
defaultCompilationHelper
.addSourceLines(
"Foo.java",
"package com.uber;",
"import org.jspecify.annotations.NullUnmarked;",
"import org.jspecify.annotations.Nullable;",
"public class Foo {",
" public Object f;",
" @NullUnmarked",
" // No error, because Foo is unmarked",
" public Foo() { }",
"}")
.doTest();
}
}

0 comments on commit ac72b68

Please sign in to comment.