Repository navigation
Honor @Contract when a @Nullable method is used as a method reference - #1679
Conversation
A method annotated @contract("null -> null; !null -> !null") returns non-null for a non-null argument, but NullAway rejected passing such a method as a method reference to a functional interface with a @nonnull parameter: checkOverriding only consulted the declared return nullness, and the contract handler was never asked on that path. Implement onMatchMethodReference in ContractHandler. When a contract clause has the consequent !null and every antecedent is _ or !null at a position where the functional interface method is guaranteed to pass a non-null value, mark the reference NONNULL via setComputedNullness - the same mechanism StreamNullabilityPropagator already uses. Fixes uber#1502
Walkthrough
Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
msridhar
left a comment
There was a problem hiding this comment.
This is a beautiful fix and a great explanation. Thank you!!
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #1679 +/- ##
============================================
- Coverage 87.82% 87.77% -0.06%
- Complexity 3131 3142 +11
============================================
Files 109 109
Lines 10562 10606 +44
Branches 2135 2150 +15
============================================
+ Hits 9276 9309 +33
- Misses 608 611 +3
- Partials 678 686 +8 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Fixes #1502.
The problem
A method annotated
@Contract("null -> null; !null -> !null")returns non-null whenever it is given a non-null argument. When such a method is passed as a method reference to a functional interface whose parameter is@NonNull, the reference is a valid implementation of that interface — but NullAway reports:The
@Contracthandler is not consulted on this path at all:NullAway#checkOverridingonly asks for the declared return nullness of the referenced method.The fix
NullAway#matchMemberReferencealready callshandler.onMatchMethodReference(...)immediately beforecheckOverriding, andcheckOverridingskips the error when the member reference has been given a computed nullness ofNONNULL:StreamNullabilityPropagator#onMatchMethodReferencealready uses exactly this mechanism to mark a reference non-null. This PR implementsonMatchMethodReferenceinContractHandlerthe same way: if some contract clause has the consequent!nulland every antecedent is either_or!nullat a position where the functional interface method is guaranteed to pass a non-null value, the reference is markedNONNULL.The per-parameter nullness of the functional interface method is computed the same way
NullAway#checkParamOverridingcomputes it:Nullness.paramHasNullableAnnotation, plusGenericsChecks#getGenericMethodParameterNullnessagainst the inferred functional interface type in JSpecify mode, so that a@Nullabletype argument is respected.Conservative by construction:
_or!null(e.g.null,true,false) disqualifies the clause.Tests
ContractsTests#contractImpliesNonNullForMethodReferenceand its JSpecify counterpart. Both fail onmasterand pass with the fix. Between them they cover five newly-accepted cases and five that must keep reporting:@Contract("null -> null; !null -> !null")→String apply(String)@Contract("_ -> !null")@Contract("!null, _ -> !null")→ two-parameter interfaceFunction<String, String>(the reported case)Fn<P extends @Nullable Object, ...>withFn<String, String>@Contract("null -> null")only@Contractat all@NullableFunction<@Nullable String, String>Fn<@Nullable String, String>The full
:nullaway:testsuite passes, and Google Java Format leaves the change unmodified.Before pressing the "Create Pull Request" button, please provide the following:
A description about what and why you are contributing, even if it's trivial.
The issue number(s) or PR number(s) in the description if you are contributing in response to those.
If applicable, unit tests.
Note on AI usage
I am a first-time contributor here, so per
CONTRIBUTING.md: I used Claude to help explore thecheckOverriding/onMatchMethodReferencecode path and to draft the handler implementation and the tests. I reviewed every line of the change myself, ran the two new tests against unpatchedmasterfirst to confirm they fail without the fix, then ran them and the full:nullaway:testsuite with the fix applied. I have read and understood all of the changes in this PR.