diff --git a/nullaway/build.gradle b/nullaway/build.gradle index 8000dbc9e3..90ac062418 100644 --- a/nullaway/build.gradle +++ b/nullaway/build.gradle @@ -152,7 +152,7 @@ tasks.register('buildWithNullAway', JavaCompile) { sourceSets.main.compileClasspath) options.errorprone.enabled = true options.errorprone { - option("NullAway:AnnotatedPackages", "com.uber,org.checkerframework.nullaway") + option("NullAway:AnnotatedPackages", "com.uber,org.checkerframework.nullaway,com.google.common") option("NullAway:CastToNonNullMethod", "com.uber.nullaway.NullabilityUtil.castToNonNull") option("NullAway:CheckOptionalEmptiness") option("NullAway:AcknowledgeRestrictiveAnnotations") diff --git a/nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java b/nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java index ae801f67e9..37a56f6d3d 100644 --- a/nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java +++ b/nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java @@ -627,14 +627,17 @@ public static Nullness getGenericReturnNullnessAtInvocation( MethodInvocationTree tree, VisitorState state, Config config) { - if (!(tree.getMethodSelect() instanceof MemberSelectTree)) { + if (!(tree.getMethodSelect() instanceof MemberSelectTree) || invokedMethodSymbol.isStatic()) { 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); + } } /** @@ -677,7 +680,7 @@ public static Nullness getGenericParameterNullnessAtInvocation( MethodInvocationTree tree, VisitorState state, Config config) { - if (!(tree.getMethodSelect() instanceof MemberSelectTree)) { + if (!(tree.getMethodSelect() instanceof MemberSelectTree) || invokedMethodSymbol.isStatic()) { return Nullness.NONNULL; } Type enclosingType = diff --git a/nullaway/src/test/java/com/uber/nullaway/NullAwayJSpecifyGenericsTests.java b/nullaway/src/test/java/com/uber/nullaway/NullAwayJSpecifyGenericsTests.java index e7227064f3..eb7c02fe65 100644 --- a/nullaway/src/test/java/com/uber/nullaway/NullAwayJSpecifyGenericsTests.java +++ b/nullaway/src/test/java/com/uber/nullaway/NullAwayJSpecifyGenericsTests.java @@ -1494,6 +1494,52 @@ public void interactionWithContracts() { .doTest(); } + @Test + public void testForStaticMethodCallAsAParam() { + makeHelper() + .addSourceLines( + "Test.java", + "package com.uber;", + "import org.jspecify.annotations.Nullable;", + "class Test {", + " static class A {", + " public static A returnA(){", + " return new A();", + " }", + " public static A returnAWithParam(Object o){", + " return new A();", + " }", + " }", + " static void func(A a){", + " }", + " static void testNegative() {", + " func(A.returnA());", + " }", + " static void testNegative2() {", + " func(A.returnAWithParam(new Object()));", + " }", + "}") + .doTest(); + } + + @Test + public void testForDiamondOperatorReturnedAsAMethodCaller() { + makeHelper() + .addSourceLines( + "Test.java", + "package com.uber;", + "import org.jspecify.annotations.Nullable;", + "class Test {", + " static class B{", + " String build(){return \"x\";}", + " }", + " static String testNegative() {", + " return new B<>().build();", + " }", + "}") + .doTest(); + } + private CompilationTestHelper makeHelper() { return makeTestHelperWithArgs( Arrays.asList(