Skip to content

Commit ddf2440

Browse files
committed
formatting
1 parent a2489ea commit ddf2440

File tree

2 files changed

+52
-40
lines changed

2 files changed

+52
-40
lines changed

nullaway/src/main/java/com/uber/nullaway/generics/GenericsChecks.java

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import static com.google.common.base.Verify.verify;
44
import static com.uber.nullaway.NullabilityUtil.castToNonNull;
5-
import static com.uber.nullaway.NullabilityUtil.findEnclosingMethodOrLambdaOrInitializer;
65

76
import com.google.errorprone.VisitorState;
87
import com.google.errorprone.suppliers.Supplier;
@@ -41,7 +40,6 @@
4140
import java.util.List;
4241
import java.util.Map;
4342
import java.util.Objects;
44-
import java.util.stream.Collectors;
4543
import javax.lang.model.type.ExecutableType;
4644
import javax.lang.model.type.TypeKind;
4745
import javax.lang.model.type.TypeVariable;
@@ -452,16 +450,19 @@ public void checkTypeParameterNullnessForAssignability(
452450
// method call has a return type of class type
453451
if (methodSymbol.getReturnType() instanceof Type.ClassType) {
454452
Type.ClassType returnType = (Type.ClassType) methodSymbol.getReturnType();
455-
List<Symbol. TypeVariableSymbol> typeParam = methodSymbol.getTypeParameters();
453+
List<Symbol.TypeVariableSymbol> typeParam = methodSymbol.getTypeParameters();
456454
List<Type> returnTypeTypeArg = returnType.getTypeArguments();
457455

458456
// if generic type in return type
459457
if (!typeParam.isEmpty()) {
460458
Map<Type, Type> genericNullness = new HashMap<>();
461459
for (int i = 0; i < typeParam.size(); i++) {
462460
Type upperBound = typeParam.get(i).type.getUpperBound();
463-
if(getTypeNullness(upperBound, config) == Nullness.NULLABLE) { // generic has nullable upperbound
464-
Type lhsInferredType = inferMethodTypeArgument(typeParam.get(i).type, lhsTypeArguments, returnTypeTypeArg, state);
461+
if (getTypeNullness(upperBound, config)
462+
== Nullness.NULLABLE) { // generic has nullable upperbound
463+
Type lhsInferredType =
464+
inferMethodTypeArgument(
465+
typeParam.get(i).type, lhsTypeArguments, returnTypeTypeArg, state);
465466
if (lhsInferredType != null) { // && has a nullable upperbound
466467
genericNullness.put(typeParam.get(i).type, lhsInferredType);
467468
}
@@ -523,22 +524,29 @@ public void checkTypeParameterNullnessForAssignability(
523524
}
524525
}
525526

526-
private Type inferMethodTypeArgument(Type typeParam, List<? extends Tree> lhsTypeArg, List<Type> typeArg, VisitorState state) {
527+
private Type inferMethodTypeArgument(
528+
Type typeParam, List<? extends Tree> lhsTypeArg, List<Type> typeArg, VisitorState state) {
527529
// base case
528-
if(typeParam == null || lhsTypeArg == null || typeArg == null) {
530+
if (typeParam == null || lhsTypeArg == null || typeArg == null) {
529531
return null;
530532
}
531533

532534
// recursive case
533535
Type inferType = null;
534-
for(int i=0; i<typeArg.size(); i++) {
536+
for (int i = 0; i < typeArg.size(); i++) {
535537
Type type = typeArg.get(i);
536-
if(state.getTypes().isSameType(typeParam, type)) {
538+
if (state.getTypes().isSameType(typeParam, type)) {
537539
return ASTHelpers.getType(lhsTypeArg.get(i));
538-
} else if(!type.getTypeArguments().isEmpty()) {
539-
// instanceof Type.ForAll TODO: check if the lhsTypeArg is a generic class? -> maybe the base case makes it unnecessary
540-
inferType = inferMethodTypeArgument(typeParam, ((ParameterizedTypeTree) lhsTypeArg.get(i)).getTypeArguments(), typeArg.get(i).getTypeArguments(), state);
541-
if(inferType != null) {
540+
} else if (!type.getTypeArguments().isEmpty()) {
541+
// instanceof Type.ForAll TODO: check if the lhsTypeArg is a generic class? -> maybe
542+
// the base case makes it unnecessary
543+
inferType =
544+
inferMethodTypeArgument(
545+
typeParam,
546+
((ParameterizedTypeTree) lhsTypeArg.get(i)).getTypeArguments(),
547+
typeArg.get(i).getTypeArguments(),
548+
state);
549+
if (inferType != null) {
542550
return inferType;
543551
}
544552
}

nullaway/src/test/java/com/uber/nullaway/jspecify/GenericMethodTests.java

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -179,15 +179,18 @@ public void genericInferenceOnAssignments() {
179179
" }",
180180
" }",
181181
" static void testLocalAssign() {",
182-
// " // legal",
183-
// " Foo<@Nullable Object> f1 = Foo.makeNull(null);",
184-
// " // BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required",
185-
// " Foo<Object> f2 = Foo.makeNull(null);",
186-
// " // ILLEGAL: U does not have a @Nullable upper bound",
187-
// " // BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required",
188-
// " Foo<@Nullable Object> f3 = Foo.makeNonNull(null);",
189-
// " // BUG: Diagnostic contains: passing @Nullable parameter 'null' where @NonNull is required",
190-
// " Foo<Object> f4 = Foo.makeNonNull(null);",
182+
// " // legal",
183+
// " Foo<@Nullable Object> f1 = Foo.makeNull(null);",
184+
// " // BUG: Diagnostic contains: passing @Nullable parameter 'null'
185+
// where @NonNull is required",
186+
// " Foo<Object> f2 = Foo.makeNull(null);",
187+
// " // ILLEGAL: U does not have a @Nullable upper bound",
188+
// " // BUG: Diagnostic contains: passing @Nullable parameter 'null'
189+
// where @NonNull is required",
190+
// " Foo<@Nullable Object> f3 = Foo.makeNonNull(null);",
191+
// " // BUG: Diagnostic contains: passing @Nullable parameter 'null'
192+
// where @NonNull is required",
193+
// " Foo<Object> f4 = Foo.makeNonNull(null);",
191194
" Foo<@Nullable Object> f5 = Foo.makeNonNull(new Object());",
192195
" }",
193196
" }")
@@ -247,24 +250,25 @@ public void genericInferenceOnAssignmentsMultipleParams() {
247250
@Test
248251
public void genericsUsedForGenericClasses() {
249252
makeHelper()
250-
.addSourceLines(
251-
"Test.java",
252-
"package com.uber;",
253-
"import org.jspecify.annotations.Nullable;",
254-
"import java.util.ArrayList;",
255-
"class Test {",
256-
" abstract class Foo<K, V> {",
257-
" abstract <U, R> Foo<U,ArrayList<R>> nonNullTest();",
258-
" abstract <U extends @Nullable Object, R extends @Nullable Object> Foo<U,ArrayList<R>> nullTest();",
259-
" }",
260-
" static void test(Foo<Void, Void> f) {",
261-
" Foo<Integer, ArrayList<String>> fooNonNull_1 = f.nonNullTest();",
262-
" Foo<Integer, ArrayList<@Nullable String>> fooNonNull_2 = f.nonNullTest();", // error message
263-
" Foo<Integer, ArrayList<String>> fooNull_1 = f.nullTest();",
264-
" Foo<Integer, ArrayList<@Nullable String>> fooNull_2 = f.nullTest();", // error message
265-
" }",
266-
"}")
267-
.doTest();
253+
.addSourceLines(
254+
"Test.java",
255+
"package com.uber;",
256+
"import org.jspecify.annotations.Nullable;",
257+
"import java.util.ArrayList;",
258+
"class Test {",
259+
" abstract class Foo<K, V> {",
260+
" abstract <U, R> Foo<U,ArrayList<R>> nonNullTest();",
261+
" abstract <U extends @Nullable Object, R extends @Nullable Object> Foo<U,ArrayList<R>> nullTest();",
262+
" }",
263+
" static void test(Foo<Void, Void> f) {",
264+
" Foo<Integer, ArrayList<String>> fooNonNull_1 = f.nonNullTest();",
265+
" Foo<Integer, ArrayList<@Nullable String>> fooNonNull_2 = f.nonNullTest();", // error message
266+
" Foo<Integer, ArrayList<String>> fooNull_1 = f.nullTest();",
267+
" Foo<Integer, ArrayList<@Nullable String>> fooNull_2 = f.nullTest();", // error
268+
// message
269+
" }",
270+
"}")
271+
.doTest();
268272
}
269273

270274
@Test

0 commit comments

Comments
 (0)