Skip to content

Commit

Permalink
Updated error handling and added tests for local and parameter arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
armughan11 committed Mar 10, 2024
1 parent f540c85 commit ef0fe1a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
1 change: 1 addition & 0 deletions nullaway/src/main/java/com/uber/nullaway/ErrorMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public enum MessageTypes {
PASS_NULLABLE_GENERIC,
WRONG_OVERRIDE_RETURN_GENERIC,
WRONG_OVERRIDE_PARAM_GENERIC,
ASSIGN_ARRAY_NULLABLE,
}

public String getMessage() {
Expand Down
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 @@ -507,8 +507,14 @@ public Description matchAssignment(AssignmentTree tree, VisitorState state) {
if (arraySymbol != null) {
boolean isElementNullable = isArrayElementNullable(arraySymbol, config);
if (!isElementNullable && mayBeNullExpr(state, expression)) {
String message = "assigning @Nullable expression to @NonNull field.";
return buildDescription(tree).setMessage(message).build();
final String message = "Writing @Nullable expression into array with @NonNull contents.";
ErrorMessage errorMessage = new ErrorMessage(MessageTypes.ASSIGN_ARRAY_NULLABLE, message);
return errorBuilder.createErrorDescription(
errorMessage,
expression,
buildDescription(expression),
state,
ASTHelpers.getSymbol(tree.getVariable()));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public void nullableAssignmentNonnullArray() {
"class Test {",
" static String [] foo = new String[10];",
" static void foo() {",
" // BUG: Diagnostic contains: assigning @Nullable expression to @NonNull field",
" // BUG: Diagnostic contains: Writing @Nullable expression into array with @NonNull contents",
" foo[1] = null;",
" }",
"}")
Expand All @@ -169,6 +169,49 @@ public void nullableAssignmentNullableArray() {
.doTest();
}

@Test
public void nullableAssignmentLocalArray() {
makeHelper()
.addSourceLines(
"Test.java",
"package com.uber;",
"import org.jspecify.annotations.Nullable;",
"class Test {",
" static void foo() {",
" String [] nonNullArray = new String[10];",
" @Nullable String [] nullableArray = new String[10];",
" // BUG: Diagnostic contains: Writing @Nullable expression into array with @NonNull contents",
" nonNullArray[1] = null;",
" // OK: since array elements are @Nullable",
" nullableArray[1] = null;",
" }",
"}")
.doTest();
}

@Test
public void nullableAssignmentParameterArray() {
makeHelper()
.addSourceLines(
"Test.java",
"package com.uber;",
"import org.jspecify.annotations.Nullable;",
"class Test {",
" static void fizz(String[] nonNullArray, @Nullable String[] nullableArray) {",
" // BUG: Diagnostic contains: Writing @Nullable expression into array with @NonNull contents",
" nonNullArray[1] = null;",
" // OK: since array elements are @Nullable",
" nullableArray[1] = null;",
" }",
" public static void main(String[] args) {",
" String[] foo = new String[10];",
" @Nullable String[] bar = new String[10];",
" fizz(foo, bar);",
" }",
"}")
.doTest();
}

private CompilationTestHelper makeHelper() {
return makeTestHelperWithArgs(
Arrays.asList(
Expand Down

0 comments on commit ef0fe1a

Please sign in to comment.