Skip to content

Fix library models check for expression-body lambdas (Fixes #910) - #1603

Closed
dyrpsf wants to merge 1 commit into
uber:masterfrom
dyrpsf:fix-lambda-library-models
Closed

Fix library models check for expression-body lambdas (Fixes #910)#1603
dyrpsf wants to merge 1 commit into
uber:masterfrom
dyrpsf:fix-lambda-library-models

Conversation

@dyrpsf

@dyrpsf dyrpsf commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

Title

Fix library models check for expression-body lambdas (Fixes #910)

Description

This PR addresses Issue #910, where NullAway was failing to report RETURN_NULLABLE errors for expression-body lambdas (e.g., (x) -> null) when a library model overrides the return type to @NonNull.

Motivation and Context

Previously, matchLambdaExpression contained an early bailout if the implemented functional interface was unannotated. However, this early exit completely bypassed the checkReturnExpression step, which is where library models (like Guava's Function) are evaluated to see if they override the return nullability.

Specific Changes

  1. Deferred Bailout: Updated matchLambdaExpression to check getMethodReturnNullness before exiting. If a library model forces the return to be NONNULL, the check continues even if the underlying functional interface is unannotated.
  2. New Test Suite: Added NullAwayLambdaTests.java with a specific test case to ensure Function<Object, Object> f1 = (x) -> null; correctly triggers a bug diagnostic.

Summary by CodeRabbit

  • New Features

    • Enhanced analysis for Java's Optional.of() and Optional.ofNullable() operations to better track non-null states.
    • Added dataflow-level tracking for optional content through assignment operations.
  • Bug Fixes

    • Improved detection of when optional content is guaranteed non-null, reducing false positives when using Optional.of().
  • Tests

    • Added test coverage for Optional.of() and Optional.ofNullable() initialization scenarios.

@coderabbitai

coderabbitai Bot commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Caution

Review failed

Pull request was closed or merged during review

Walkthrough

This PR extends NullAway's handler plugin system with a new dataflow callback for assignment-node visitation. The core infrastructure adds onDataflowVisitAssignment(...) as a default method in the Handler interface, wires delegation through CompositeHandler, and invokes it from AccessPathNullnessPropagation. OptionalEmptinessHandler implements this hook to track Optional factory methods (Optional.of and Optional.ofNullable) and propagate non-null facts through assignments, including unwrapping wrapper nodes like casts and null-checks. The implementation unifies optional-method detection via a shared matcher and exposes a public accessor for the optional-content tracking element. A new test validates the behavior for both factory methods and nullable arguments.

Suggested reviewers

  • yuxincs
  • lazaroclapp
🚥 Pre-merge checks | ✅ 1 | ❌ 4

❌ Failed checks (4 warnings)

Check name Status Explanation Resolution
Title check ⚠️ Warning The PR title references fixing lambda library models checks (#910), but the actual changes implement dataflow hooks for optional emptiness analysis unrelated to lambdas. Update the PR title to accurately reflect the actual changes, such as 'Add dataflow hook for optional emptiness tracking' or provide clarification about why lambda fixes are not included.
Linked Issues check ⚠️ Warning The PR objectives claim to fix lambda library model checks (#910), but the actual code changes implement optional emptiness dataflow features with no lambda-related modifications. Either implement the lambda fixes described in #910 or update the linked issue to match the actual optional emptiness enhancements being delivered.
Out of Scope Changes check ⚠️ Warning All code changes relate to optional emptiness dataflow propagation, which is completely out of scope for the stated objective of fixing lambda library model checks. Align PR scope with stated objectives by either implementing lambda fixes or updating the PR title and linked issues to reflect optional emptiness work.
Docstring Coverage ⚠️ Warning Docstring coverage is 13.04% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (1 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@dyrpsf
dyrpsf force-pushed the fix-lambda-library-models branch from 303e0e3 to e55aca8 Compare June 13, 2026 10:47
@dyrpsf

dyrpsf commented Jun 13, 2026

Copy link
Copy Markdown
Contributor Author

Closing this PR due to some technical issues,
I will open a new PR for the same changes.

@dyrpsf dyrpsf closed this Jun 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Missing check for library models for lambdas with an expression body

1 participant