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 errors for writes to @NullUnmarked fields #1102

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}
Loading