Skip to content

Commit

Permalink
Tests for loops over arrays (#982)
Browse files Browse the repository at this point in the history
  • Loading branch information
msridhar authored Jun 19, 2024
1 parent 8593fe2 commit 4f6f21e
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions nullaway/src/test/java/com/uber/nullaway/jspecify/ArrayTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.errorprone.CompilationTestHelper;
import com.uber.nullaway.NullAwayTestsBase;
import java.util.Arrays;
import org.junit.Ignore;
import org.junit.Test;

public class ArrayTests extends NullAwayTestsBase {
Expand Down Expand Up @@ -470,6 +471,51 @@ public void localVariableIndexArray() {
.doTest();
}

@Test
public void loopVariableIndex() {
makeHelper()
.addSourceLines(
"Test.java",
"package com.uber;",
"import org.jspecify.annotations.Nullable;",
"class Test {",
" static @Nullable String[] fizz = {\"1\"};",
" static void foo() {",
" for (int i = 0; i < fizz.length; i++) {",
" if (fizz[i] != null) {",
" fizz[i].toString();",
" }",
" }",
" }",
"}")
.doTest();
}

@Test
@Ignore("for-each handling needs to be fixed; see https://github.com/uber/NullAway/issues/983")
public void forEachLoop() {
makeHelper()
.addSourceLines(
"Test.java",
"package com.uber;",
"import org.jspecify.annotations.Nullable;",
"class Test {",
" static @Nullable String[] fizz = {\"1\"};",
" static void foo() {",
" for (String s : fizz) {",
" if (s != null) {",
" s.toString();",
" }",
" }",
" for (String s : fizz) {",
" // BUG: Diagnostic contains: dereferenced expression s is @Nullable",
" s.toString();",
" }",
" }",
"}")
.doTest();
}

@Test
public void methodInvocationIndexArray() {
makeHelper()
Expand Down

0 comments on commit 4f6f21e

Please sign in to comment.