Commit 4b4d1e8
authored
JSpecify: Apply annotations on type variables to lambdas in more cases (#1428)
Consider this example:
```java
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;
import java.util.function.Function;
@NullMarked
class Test {
static class Foo<T> {
T doSomething(Function<@nullable T, T> f) {
return f.apply(null);
}
}
void test(Foo<String> foo) {
String s =
foo.doSomething(
t -> {
return "length: " + t.length();
});
}
}
```
NullAway should produce an error at `t.length()` since `t` may be null.
Before this PR, NullAway would not give an error, since for a
`Foo<String> foo`, it treated the type of the formal parameter as
`Function<String, String>`, without considering the `@Nullable`
annotation in `Function<@nullable T, T>`. This PR closes that gap, both
for passing lambdas as parameters and for assigning them to fields.
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **Improvements**
* Enhanced nullability analysis for lambda expressions in generic type
contexts.
* Improved detection of null-dereference issues when lambdas use generic
type variables.
* Better nullability tracking for lambdas assigned to fields or used as
method arguments and return values.
<sub>✏️ Tip: You can customize this high-level summary in your review
settings.</sub>
<!-- end of auto-generated comment: release notes by coderabbit.ai -->1 parent cdb7675 commit 4b4d1e8
2 files changed
Lines changed: 151 additions & 2 deletions
File tree
- nullaway/src
- main/java/com/uber/nullaway/generics
- test/java/com/uber/nullaway/jspecify
Lines changed: 45 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
419 | 419 | | |
420 | 420 | | |
421 | 421 | | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
422 | 432 | | |
423 | 433 | | |
424 | 434 | | |
| |||
586 | 596 | | |
587 | 597 | | |
588 | 598 | | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
589 | 604 | | |
590 | 605 | | |
591 | 606 | | |
| |||
601 | 616 | | |
602 | 617 | | |
603 | 618 | | |
| 619 | + | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
604 | 627 | | |
605 | 628 | | |
606 | 629 | | |
| |||
609 | 632 | | |
610 | 633 | | |
611 | 634 | | |
612 | | - | |
| 635 | + | |
613 | 636 | | |
614 | 637 | | |
615 | 638 | | |
| |||
1293 | 1316 | | |
1294 | 1317 | | |
1295 | 1318 | | |
1296 | | - | |
| 1319 | + | |
| 1320 | + | |
1297 | 1321 | | |
1298 | 1322 | | |
1299 | 1323 | | |
| |||
2036 | 2060 | | |
2037 | 2061 | | |
2038 | 2062 | | |
| 2063 | + | |
| 2064 | + | |
| 2065 | + | |
| 2066 | + | |
| 2067 | + | |
| 2068 | + | |
| 2069 | + | |
| 2070 | + | |
| 2071 | + | |
| 2072 | + | |
| 2073 | + | |
| 2074 | + | |
| 2075 | + | |
| 2076 | + | |
| 2077 | + | |
| 2078 | + | |
| 2079 | + | |
| 2080 | + | |
| 2081 | + | |
2039 | 2082 | | |
2040 | 2083 | | |
2041 | 2084 | | |
| |||
Lines changed: 106 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
0 commit comments