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

Tests for loops over arrays #982

Merged
merged 3 commits into from
Jun 19, 2024
Merged
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
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",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting... wonder how we are representing the access paths here. Might be a bit much to ask (basically, you need NullAway to remember "we checked all elements of this array and they are all @NonNull, therefore..."

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, maybe we should clarify this test. It's a bit confusing due to the multiple loops in the same method. This was just meant to test that if the array contents are @Nullable, the loop variable should be treated as @Nullable (which it currently isn't; that why the test is ignored). I wasn't thinking about using reasoning from the previous loop to avoid warning on the second warning.

@armughan11 when you work on #983, please rewrite this test to reorder the loops, to avoid this confusion

" s.toString();",
" }",
" }",
"}")
.doTest();
}

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