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

JSpecify: Handle @Nullable elements for enhanced-for-loops on arrays #986

Merged
merged 2 commits into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.google.errorprone.suppliers.Supplier;
import com.google.errorprone.suppliers.Suppliers;
import com.google.errorprone.util.ASTHelpers;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.MethodInvocationTree;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Type;
Expand Down Expand Up @@ -507,7 +508,6 @@ public TransferResult<Nullness, NullnessStore> visitAssignment(
Node rhs = node.getExpression();
Nullness value = values(input).valueOfSubNode(rhs);
Node target = node.getTarget();

Copy link
Collaborator

Choose a reason for hiding this comment

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

remove this whitespace change

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

if (target instanceof LocalVariableNode
&& !castToNonNull(ASTHelpers.getType(target.getTree())).isPrimitive()) {
LocalVariableNode localVariableNode = (LocalVariableNode) target;
Expand Down Expand Up @@ -791,8 +791,14 @@ public TransferResult<Nullness, NullnessStore> visitArrayAccess(
Nullness resultNullness;
// Unsoundly assume @NonNull, except in JSpecify mode where we check the type
if (config.isJSpecifyMode()) {
Symbol arraySymbol = ASTHelpers.getSymbol(node.getArray().getTree());
Symbol arraySymbol;
boolean isElementNullable = false;
ExpressionTree arrayExpr = node.getArrayExpression();
msridhar marked this conversation as resolved.
Show resolved Hide resolved
if (arrayExpr != null) {
arraySymbol = ASTHelpers.getSymbol(arrayExpr);
} else {
arraySymbol = ASTHelpers.getSymbol(node.getArray().getTree());
}
if (arraySymbol != null) {
isElementNullable = NullabilityUtil.isArrayElementNullable(arraySymbol, config);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
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 @@ -492,7 +491,6 @@ public void loopVariableIndex() {
}

@Test
@Ignore("for-each handling needs to be fixed; see https://github.com/uber/NullAway/issues/983")
public void forEachLoop() {
makeHelper()
.addSourceLines(
Expand All @@ -506,8 +504,6 @@ public void forEachLoop() {
" if (s != null) {",
" s.toString();",
" }",
" }",
" for (String s : fizz) {",
" // BUG: Diagnostic contains: dereferenced expression s is @Nullable",
" s.toString();",
" }",
Expand Down