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

Handle conditional expressions like x.isEmpty() ? null : null #6786

Merged
merged 2 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -2723,11 +2723,13 @@ public Node visitConditionalExpression(ConditionalExpressionTree tree, Void p) {
// see JLS 15.25
TypeMirror exprType = TreeUtils.typeOf(tree);
if (exprType.getKind() == TypeKind.NULL) {
// Happens when the 2nd and 3rd operands are both null, i.e. b ? null : null.
// Happens when the 2nd and 3rd operands are both null, e.g.: b ? null : null
Tree parent = TreePathUtil.getContextForPolyExpression(getCurrentPath());
if (parent != null) {
exprType = TreeUtils.typeOf(parent);
} else {
// exprType is null when the condition is non-atomic, e.g.: x.isEmpty() ? null : null
}
if (parent == null || exprType == null) {
exprType = TypesUtils.getObjectTypeMirror(env);
}
}
Expand Down
7 changes: 7 additions & 0 deletions framework/tests/all-systems/Issue6785.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import org.checkerframework.checker.nullness.qual.Nullable;

public class Issue6785 {
@Nullable String foo2(String x) {
return x.isEmpty() ? null : null;
}
}