Skip to content

Commit 18a0a68

Browse files
authored
Properly handle conditional expression within parens as RHS of assignment (#1140)
Fixes #1127 Our previous (hacky) logic for determining the type of a conditional expression on the RHS of an assignment did not account for the expression possibly being enclosed in parentheses.
1 parent 90265c5 commit 18a0a68

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.sun.source.tree.NewArrayTree;
2020
import com.sun.source.tree.NewClassTree;
2121
import com.sun.source.tree.ParameterizedTypeTree;
22+
import com.sun.source.tree.ParenthesizedTree;
2223
import com.sun.source.tree.Tree;
2324
import com.sun.source.tree.VariableTree;
2425
import com.sun.source.util.TreePath;
@@ -584,7 +585,12 @@ public static void checkTypeParameterNullnessForConditionalExpression(
584585
ConditionalExpressionTree tree, VisitorState state) {
585586
// hack: sometimes array nullability doesn't get computed correctly for a conditional expression
586587
// on the RHS of an assignment. So, look at the type of the assignment tree.
587-
Tree parent = state.getPath().getParentPath().getLeaf();
588+
TreePath parentPath = state.getPath().getParentPath();
589+
Tree parent = parentPath.getLeaf();
590+
while (parent instanceof ParenthesizedTree) {
591+
parentPath = parentPath.getParentPath();
592+
parent = parentPath.getLeaf();
593+
}
588594
if (parent instanceof AssignmentTree || parent instanceof VariableTree) {
589595
return getTreeType(parent, state);
590596
}

nullaway/src/test/java/com/uber/nullaway/jspecify/GenericsTests.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2045,6 +2045,21 @@ public void issue1093() {
20452045
.doTest();
20462046
}
20472047

2048+
@Test
2049+
public void issue1127() {
2050+
makeHelper()
2051+
.addSourceLines(
2052+
"Main.java",
2053+
"package com.uber;",
2054+
"import org.jspecify.annotations.Nullable;",
2055+
"public class Main {",
2056+
" void arrayAssign(boolean b, @Nullable String @Nullable [] vals) {",
2057+
" @Nullable String[] arr = (b ? vals : null);",
2058+
" }",
2059+
"}")
2060+
.doTest();
2061+
}
2062+
20482063
@Test
20492064
public void nullUnmarkedGenericField() {
20502065
makeHelper()

0 commit comments

Comments
 (0)