Repository navigation
Fix crash in map callbacks after an unmarked or suppressed filter predicate - #1996
Conversation
These tests fail with IllegalStateException: null filterNullStore for tree. Assisted-by: Claude Code (claude-opus-5-5)
…dicate
When the `filter` predicate of a java.util.stream or RxJava chain was an anonymous class whose `test` method is `@NullUnmarked` or `@SuppressWarnings("NullAway")`, NullAway crashed while checking the `map` callback after it, and compilation failed with:
java.lang.IllegalStateException: null filterNullStore for tree
StreamNullabilityPropagator records the facts that hold when a filter predicate returns true, and records them only for a predicate NullAway checks. A predicate in unmarked or suppressed code leaves no record, and neither does a checked predicate whose every return is the literal `false`. Both readers of the record, the initial store of a `map` lambda or anonymous method and an unbound method reference passed to `map`, threw on the missing record.
They now add no facts in that case, so the callback is checked as if no filter preceded it. A callback after an always-false predicate never runs, so a dereference in it may be reported; adding no facts is the conservative choice and is the same as for an unchecked predicate.
Caller sees: `dereferenced expression '<expr>' is @Nullable` or `referenced method returns @Nullable` where a `map` callback relies on a null check in such a predicate, instead of the crash.
Tests cover Stream and RxJava, both annotations, the method reference path, and an always-false lambda predicate, each next to a checked-predicate control that keeps its facts; the non-control tests fail on master with the exception above.
Fixes uber#1990
Assisted-by: Claude Code (claude-opus-5-5)
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
Walkthrough
Suggested reviewers: Priority: ➖ Normal Change: Bug fix · Severity of issue fixed: Medium Merge Risk: ⚪ Minimal · up to
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1996 +/- ##
============================================
+ Coverage 88.05% 88.10% +0.04%
Complexity 38 38
============================================
Files 117 117
Lines 11941 11940 -1
Branches 2444 2444
============================================
+ Hits 10515 10520 +5
+ Misses 647 644 -3
+ Partials 779 776 -3 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
…literals (#2002) Parentheses around a lambda, a method reference, a stream receiver, or a boolean literal produced false positives, because several handlers matched those trees with `instanceof` on the tree as written, and a `ParenthesizedTree` matched none of their cases. For example, `s.filter((b -> b.get() != null)).map(b -> b.get().length())` reported `dereferenced expression 'b.get()' is @Nullable`, and an `@EnsuresNonNullIf("item")` method that ends in `return (false);` after `if (item != null) { return (true); }` reported `Method is annotated with @EnsuresNonNullIf but does not ensure fields [item]`. The same code without the parentheses compiles cleanly. The handlers now strip parentheses before they match, at these sites: - `StreamNullabilityPropagator`: the argument of `filter` and `map` (lambda, method reference, or anonymous class); the receiver that links a stream call to the call before it, as in `(s.filter(...)).map(...)`; the argument of `collect` and the lambdas passed to the collector factory; the boolean literal a filter returns; and, in JSpecify mode, the method reference that marks a filter as null-rejecting, as in `filter((Objects::nonNull))`. - `EnsuresNonNullIfHandler`: the boolean literal a return statement returns. - `SynchronousCallbackHandler`: a parenthesized lambda passed to a method whose callbacks run synchronously, such as `Iterable.forEach`, gets the access paths of the enclosing method, as an unparenthesized one does. The other `instanceof` checks in the handlers either receive expressions through `NullAway.mayBeNullExpr`, which already strips parentheses and casts, or test dataflow nodes, which carry no parentheses. Caller sees: - `java.lang.IllegalStateException: null filterNullStore for tree` when a stream filter can only return `(false)`, as plain `return false;` already does on master; #1996 removes that exception for both forms. - No `dereferenced expression ... is @Nullable`, `incompatible types`, or `Method is annotated with @EnsuresNonNullIf but does not ensure fields` warning for the parenthesized forms above. Each new test fails on master with the false positive and passes with this change; each fixed site has its own test, and controls with the same parentheses but no null check still report. Part of #1993 (groups B and C). Casts (group A) are #1997 and the parenthesized arguments to unmarked code in `GenericsChecks` (group D) are #1999. #1996 also edits `StreamNullabilityPropagator`, in `onDataflowInitialStore` and `onMatchMethodReference`, which this change does not touch. Assisted-by: Claude Code (claude-opus-5-5) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Improved nullability analysis for expressions enclosed in parentheses. * Stream, reactive, callback, and `forEach` expressions now correctly preserve nullness information when parenthesized. * Parenthesized boolean literals are now validated correctly in nullability contracts. * Collector handling, chained calls, method references, and lambda expressions now recognize equivalent parenthesized forms. * **Tests** * Added coverage for parenthesized Stream, RxJava, callback, lambda, collector, and contract expressions. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
When the
filterpredicate of a java.util.stream or RxJava chain was an anonymous class whosetestmethod is@NullUnmarkedor@SuppressWarnings("NullAway"), NullAway crashed while checking themapcallback after it, and compilation failed with:StreamNullabilityPropagator records the facts that hold when a filter predicate returns true, and records them only for a predicate NullAway checks. A predicate in unmarked or suppressed code leaves no record, and neither does a checked predicate whose every return is the literal
false. Both readers of the record, the initial store of amaplambda or anonymous method and an unbound method reference passed tomap, threw on the missing record.They now add no facts in that case, so the callback is checked as if no filter preceded it. A callback after an always-false predicate never runs, so a dereference in it may be reported; adding no facts is the conservative choice and is the same as for an unchecked predicate.
Caller sees:
dereferenced expression '<expr>' is @Nullableorreferenced method returns @Nullablewhere amapcallback relies on a null check in such a predicate, instead of the crash.Tests cover Stream and RxJava, both annotations, the method reference path, and an always-false lambda predicate, each next to a checked-predicate control that keeps its facts; the non-control tests fail on master with the exception above.
Fixes #1990
Assisted-by: Claude Code (claude-opus-5-5)
Summary by CodeRabbit
Bug Fixes
Tests
filter/mapscenarios, including method references.