Skip to content

Commit

Permalink
Don't report errors for writes to @NullUnmarked fields (#1102)
Browse files Browse the repository at this point in the history
Fixes #1101
  • Loading branch information
msridhar authored Dec 19, 2024
1 parent 41c7c55 commit b7dad0c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
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 @@ -485,6 +485,11 @@ public Description matchAssignment(AssignmentTree tree, VisitorState state) {
if (lhsType != null && lhsType.isPrimitive()) {
doUnboxingCheck(state, tree.getExpression());
}
Symbol assigned = ASTHelpers.getSymbol(tree.getVariable());
if (assigned != null && codeAnnotationInfo.isSymbolUnannotated(assigned, config, handler)) {
// assigning to symbol that is unannotated
return Description.NO_MATCH;
}
// generics check
if (lhsType != null && config.isJSpecifyMode()) {
GenericsChecks.checkTypeParameterNullnessForAssignability(tree, this, state);
Expand All @@ -508,7 +513,6 @@ public Description matchAssignment(AssignmentTree tree, VisitorState state) {
}
}

Symbol assigned = ASTHelpers.getSymbol(tree.getVariable());
if (assigned == null || assigned.getKind() != ElementKind.FIELD) {
// not a field of nullable type
return Description.NO_MATCH;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2045,6 +2045,30 @@ public void issue1093() {
.doTest();
}

@Test
public void nullUnmarkedGenericField() {
makeHelper()
.addSourceLines(
"Test.java",
"package com.unannotated;",
"import org.jspecify.annotations.*;",
"import java.util.function.Function;",
"public class Test {",
" static Function<@Nullable String, @Nullable String> foo;",
" @NullMarked",
" static class Inner {",
" static @Nullable Function<@Nullable String, @Nullable String> bar;",
" void bar(Function<String,String> f) {",
" // no error since foo is in @NullUnmarked code",
" foo = f;",
" // BUG: Diagnostic contains: Cannot assign from type Function<String, String>",
" bar = f;",
" }",
" }",
"}")
.doTest();
}

private CompilationTestHelper makeHelper() {
return makeTestHelperWithArgs(
Arrays.asList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1173,4 +1173,25 @@ public void nullUnmarkedOnConstructorSuppressesInitializerWarnings() {
"}")
.doTest();
}

@Test
public void writeToNullUnmarkedField() {
defaultCompilationHelper
.addSourceLines(
"Test.java",
"package com.unannotated;",
"import org.jspecify.annotations.NullMarked;",
"public class Test {",
" static Object foo;",
" @NullMarked",
" static class Inner {",
" String bar() {",
" // expecting no errors since foo is in @NullUnmarked code",
" foo = null;",
" return foo.toString();",
" }",
" }",
"}")
.doTest();
}
}

0 comments on commit b7dad0c

Please # to comment.