Skip to content

Commit

Permalink
Adding test case and relevant logic to handle when a returned diamond…
Browse files Browse the repository at this point in the history
… operator object is calling a method
  • Loading branch information
akulk022 committed Nov 9, 2023
1 parent 5aeb32c commit 95d3e8e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
12 changes: 12 additions & 0 deletions nullaway/src/main/java/com/uber/nullaway/GenericsChecks.java
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,18 @@ public static Nullness getGenericReturnNullnessAtInvocation(
if (!(tree.getMethodSelect() instanceof MemberSelectTree) || invokedMethodSymbol.isStatic()) {
return Nullness.NONNULL;
}
Tree expressionTree = ((MemberSelectTree) tree.getMethodSelect()).getExpression();
if (expressionTree instanceof NewClassTree
&& ((NewClassTree) expressionTree).getIdentifier() instanceof ParameterizedTypeTree) {
ParameterizedTypeTree paramTypedTree =
(ParameterizedTypeTree) ((NewClassTree) expressionTree).getIdentifier();
// The case of having a diamond operator
if (paramTypedTree.getTypeArguments().isEmpty()) {
// bail out
// TODO: support diamond operators
return Nullness.NONNULL;
}
}
Type methodReceiverType =
castToNonNull(
getTreeType(((MemberSelectTree) tree.getMethodSelect()).getExpression(), state));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1522,6 +1522,24 @@ public void testForStaticMethodCallAsAParam() {
.doTest();
}

@Test
public void testForDiamondOperatorReturnedAsAMethodCaller() {
makeHelper()
.addSourceLines(
"Test.java",
"package com.uber;",
"import org.jspecify.annotations.Nullable;",
"class Test {",
" static class B<T>{",
" String build(){return \"x\";}",
" }",
" static String testNegative() {",
" return new B<>().build();",
" }",
"}")
.doTest();
}

private CompilationTestHelper makeHelper() {
return makeTestHelperWithArgs(
Arrays.asList(
Expand Down

0 comments on commit 95d3e8e

Please sign in to comment.