Skip to content

Commit

Permalink
JSpecify: handling the return of a diamond operator anonymous object …
Browse files Browse the repository at this point in the history
…method caller (uber#858)

In reference to the
[exception](uber#791 (comment))
mentioned in the discussion for uber#791 .

Adding the test case to reproduce the same issue:

```java
class Test {
    static class B<T>{
        String build(){return "x";}
    }
    static String testNegative() {
        //We were getting the aforementioned mentioned exception when we tried to do this
        return new B<>().build();
    }
}
```
All unit tests have passed for the changes that were made for this.

---------

Co-authored-by: Manu Sridharan <[email protected]>
  • Loading branch information
akulk022 and msridhar authored Nov 13, 2023
1 parent 0141aca commit 4af912d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
11 changes: 7 additions & 4 deletions nullaway/src/main/java/com/uber/nullaway/GenericsChecks.java
Original file line number Diff line number Diff line change
Expand Up @@ -788,10 +788,13 @@ public static Nullness getGenericReturnNullnessAtInvocation(
return Nullness.NONNULL;
}
Type methodReceiverType =
castToNonNull(
getTreeType(((MemberSelectTree) tree.getMethodSelect()).getExpression(), state));
return getGenericMethodReturnTypeNullness(
invokedMethodSymbol, methodReceiverType, state, config);
getTreeType(((MemberSelectTree) tree.getMethodSelect()).getExpression(), state);
if (methodReceiverType == null) {
return Nullness.NONNULL;
} else {
return getGenericMethodReturnTypeNullness(
invokedMethodSymbol, methodReceiverType, state, config);
}
}

/**
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 4af912d

Please sign in to comment.