Skip to content

Commit ac72b68

Browse files
authored
Don't report initializer warnings on @NullUnmarked constructors / methods (#997)
Fixes #995
1 parent 40576b7 commit ac72b68

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

nullaway/src/main/java/com/uber/nullaway/ErrorBuilder.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
package com.uber.nullaway;
2424

25+
import static com.uber.nullaway.ASTHelpersBackports.hasDirectAnnotationWithSimpleName;
2526
import static com.uber.nullaway.ASTHelpersBackports.isStatic;
2627
import static com.uber.nullaway.ErrorMessage.MessageTypes.FIELD_NO_INIT;
2728
import static com.uber.nullaway.ErrorMessage.MessageTypes.GET_ON_EMPTY_OPTIONAL;
@@ -415,7 +416,9 @@ void reportInitializerError(
415416
// Check needed here, despite check in hasPathSuppression because initialization
416417
// checking happens at the class-level (meaning state.getPath() might not include the
417418
// method itself).
418-
if (symbolHasSuppressWarningsAnnotation(methodSymbol, INITIALIZATION_CHECK_NAME)) {
419+
if (symbolHasSuppressWarningsAnnotation(methodSymbol, INITIALIZATION_CHECK_NAME)
420+
|| hasDirectAnnotationWithSimpleName(
421+
methodSymbol, NullabilityUtil.NULLUNMARKED_SIMPLE_NAME)) {
419422
return;
420423
}
421424
Tree methodTree = getTreesInstance(state).getTree(methodSymbol);

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,4 +1156,21 @@ public void dotClassSanityTest2() {
11561156
"}")
11571157
.doTest();
11581158
}
1159+
1160+
@Test
1161+
public void nullUnmarkedOnConstructorSuppressesInitializerWarnings() {
1162+
defaultCompilationHelper
1163+
.addSourceLines(
1164+
"Foo.java",
1165+
"package com.uber;",
1166+
"import org.jspecify.annotations.NullUnmarked;",
1167+
"import org.jspecify.annotations.Nullable;",
1168+
"public class Foo {",
1169+
" public Object f;",
1170+
" @NullUnmarked",
1171+
" // No error, because Foo is unmarked",
1172+
" public Foo() { }",
1173+
"}")
1174+
.doTest();
1175+
}
11591176
}

0 commit comments

Comments
 (0)