| 1 |
// Test file for nullness parameter and return checks. |
| 2 |
|
| 3 |
import org.checkerframework.checker.nullness.qual.*; |
| 4 |
|
| 5 |
interface Noop { |
| 6 |
void noop(); |
| 7 |
} |
| 8 |
|
| 9 |
interface FunctionNull<T extends @Nullable Object, R> { |
| 10 |
R apply(T t); |
| 11 |
} |
| 12 |
|
| 13 |
interface Supplier<R extends @Nullable Object> { |
| 14 |
R supply(); |
| 15 |
} |
| 16 |
|
| 17 |
interface BiFunctionNull<T, U, R> { |
| 18 |
R apply(T t, U u); |
| 19 |
} |
| 20 |
|
| 21 |
class LambdaNullness { |
| 22 |
|
| 23 |
// Annotations in lamba expressions, in static, instance of fields initializers are stored on |
| 24 |
// the last declared constructor. |
| 25 |
// |
| 26 |
// For example, the annotation for @Nullable Integer x on f7's initializer |
| 27 |
// is stored on here because it is the last defined constructor. |
| 28 |
// |
| 29 |
// See TypeFromElement::annotateParam |
| 30 |
LambdaNullness(FunctionNull<String, String> f, Object e) {} |
| 31 |
|
| 32 |
// No parameters; result is void |
| 33 |
Noop f1 = () -> {}; |
| 34 |
// No parameters, expression body |
| 35 |
Supplier<Integer> f2a = () -> 42; |
| 36 |
|
| 37 |
// No parameters, expression body |
| 38 |
// :: error: (return.type.incompatible) |
| 39 |
Supplier<Integer> f2b = () -> null; |
| 40 |
|
| 41 |
// No parameters, expression body |
| 42 |
Supplier<@Nullable Void> f3 = () -> null; |
| 43 |
// No parameters, block body with return |
| 44 |
Supplier<Integer> f4a = |
| 45 |
() -> { |
| 46 |
return 42; |
| 47 |
}; |
| 48 |
// No parameters, block body with return |
| 49 |
Supplier<@Nullable Integer> f4b = |
| 50 |
() -> { |
| 51 |
// :: error: (assignment.type.incompatible) |
| 52 |
@NonNull String s = null; |
| 53 |
|
| 54 |
return null; |
| 55 |
}; |
| 56 |
// No parameters, void block body |
| 57 |
Noop f5 = |
| 58 |
() -> { |
| 59 |
System.gc(); |
| 60 |
}; |
| 61 |
|
| 62 |
// Complex block body with returns |
| 63 |
Supplier<Integer> f6 = |
| 64 |
() -> { |
| 65 |
if (true) { |
| 66 |
return 12; |
| 67 |
} else { |
| 68 |
int result = 15; |
| 69 |
for (int i = 1; i < 10; i++) { |
| 70 |
result *= i; |
| 71 |
} |
| 72 |
// :: error: (return.type.incompatible) |
| 73 |
return null; |
| 74 |
} |
| 75 |
}; |
| 76 |
|
| 77 |
// Single declared-type parameter |
| 78 |
FunctionNull<@Nullable Integer, Integer> f7 = (@Nullable Integer x) -> 1; |
| 79 |
|
| 80 |
// Single declared-type parameter |
| 81 |
FunctionNull<@Nullable String, String> f9 = |
| 82 |
// :: error: (lambda.param.type.incompatible) |
| 83 |
(@NonNull String x) -> { |
| 84 |
return x + ""; |
| 85 |
}; |
| 86 |
// Single inferred-type parameter |
| 87 |
FunctionNull<@NonNull Integer, Integer> f10 = (x) -> x + 1; |
| 88 |
// Parentheses optional for single |
| 89 |
FunctionNull<@Nullable Integer, Integer> f11 = x -> 1; |
| 90 |
|
| 91 |
// Multiple declared-type parameters |
| 92 |
BiFunctionNull<Integer, Integer, Integer> f16 = |
| 93 |
(@Nullable Integer x, final Integer y) -> { |
| 94 |
x = null; |
| 95 |
// :: error: (unboxing.of.nullable) |
| 96 |
return x + y; |
| 97 |
}; |
| 98 |
|
| 99 |
// Multiple inferred-type parameters |
| 100 |
BiFunctionNull<String, String, String> f18 = (x, y) -> x + y; |
| 101 |
|
| 102 |
// Infer based on context. |
| 103 |
FunctionNull<@Nullable String, String> fn = |
| 104 |
(s) -> { |
| 105 |
// :: error: (dereference.of.nullable) |
| 106 |
s.toString(); |
| 107 |
return ""; |
| 108 |
}; |
| 109 |
} |