Skip to content

Commit

Permalink
Properly handle conditional expression within parens as RHS of assign…
Browse files Browse the repository at this point in the history
…ment (#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.
  • Loading branch information
msridhar authored Jan 31, 2025
1 parent 90265c5 commit 18a0a68
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.sun.source.tree.NewArrayTree;
import com.sun.source.tree.NewClassTree;
import com.sun.source.tree.ParameterizedTypeTree;
import com.sun.source.tree.ParenthesizedTree;
import com.sun.source.tree.Tree;
import com.sun.source.tree.VariableTree;
import com.sun.source.util.TreePath;
Expand Down Expand Up @@ -584,7 +585,12 @@ public static void checkTypeParameterNullnessForConditionalExpression(
ConditionalExpressionTree tree, VisitorState state) {
// hack: sometimes array nullability doesn't get computed correctly for a conditional expression
// on the RHS of an assignment. So, look at the type of the assignment tree.
Tree parent = state.getPath().getParentPath().getLeaf();
TreePath parentPath = state.getPath().getParentPath();
Tree parent = parentPath.getLeaf();
while (parent instanceof ParenthesizedTree) {
parentPath = parentPath.getParentPath();
parent = parentPath.getLeaf();
}
if (parent instanceof AssignmentTree || parent instanceof VariableTree) {
return getTreeType(parent, state);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2045,6 +2045,21 @@ public void issue1093() {
.doTest();
}

@Test
public void issue1127() {
makeHelper()
.addSourceLines(
"Main.java",
"package com.uber;",
"import org.jspecify.annotations.Nullable;",
"public class Main {",
" void arrayAssign(boolean b, @Nullable String @Nullable [] vals) {",
" @Nullable String[] arr = (b ? vals : null);",
" }",
"}")
.doTest();
}

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

0 comments on commit 18a0a68

Please sign in to comment.