Skip to content

Fix Optional factory method tracking (Partial fix for #889)#1600

Open
dyrpsf wants to merge 3 commits into
uber:masterfrom
dyrpsf:fix-optional-emptiness-tracking
Open

Fix Optional factory method tracking (Partial fix for #889)#1600
dyrpsf wants to merge 3 commits into
uber:masterfrom
dyrpsf:fix-optional-emptiness-tracking

Conversation

@dyrpsf

@dyrpsf dyrpsf commented Jun 11, 2026

Copy link
Copy Markdown

Description

This PR addresses the false positives in Optional emptiness tracking related to factory method initializations, addressing the primary concern raised in #889.

Motivation and Context

In #889, it was reported that CheckOptionalEmptiness ignores the initial value passed to an Optional factory (e.g., Optional.of and Optional.ofNullable), leading to false positives when developers safely call .get() on those variables. Previously, NullAway dropped emptiness tracking facts when an Optional was assigned to a variable or initialized inline.

Specific Changes

  1. New Handler Hook: Introduced onDataflowVisitAssignment in the Handler interface (and CompositeHandler / AccessPathNullnessPropagation) to allow plugins to intercept assignment nodes.
  2. Robust Unwrapping: Implemented the new hook in OptionalEmptinessHandler to peel back implicit compiler wrappers (TypeCastNode, NullChkNode, WideningConversionNode, NarrowingConversionNode) to safely reach the underlying MethodInvocationNode during assignments.
  3. Factory Method Support: Added tracking for Optional.of(...) (always non-empty) and Optional.ofNullable(...) (non-empty if the argument is @NonNull).
  4. CFG Tree Check Fix: Removed a restrictive base.getTree() != null check in updateNonNullAPsForOptionalContent that was silently dropping tracking facts for inline variable initializations.
  5. Test Suite Updates: Added the explicit @Nullable annotation to the parameter in logOptionalOfNullableWithNullable to align the test expectations, and ensured Optional.of flows cleanly.

Note: This addresses the factory initialization cases for #889. The trickier .or() fallback provider case mentioned in the issue may require additional modeling in a future PR, but this lays the necessary architectural groundwork.

Summary by CodeRabbit

  • New Features

    • Improved null-safety analysis: assignment sites are now analyzed with additional propagation hooks, improving detection and propagation of non-null facts through assignments and common RHS conversions.
  • Bug Fixes / Improvements

    • More accurate tracking for Optional.of()/Optional.ofNullable(), reducing false positives for Optional.get().
  • Tests

    • Added unit tests validating Optional.of and Optional.ofNullable initialization and diagnostics.

@coderabbitai

coderabbitai Bot commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 215698b9-b449-4711-801b-3553a87a409a

📥 Commits

Reviewing files that changed from the base of the PR and between 93f9d5f and 3b4be7b.

📒 Files selected for processing (1)
  • nullaway/src/main/java/com/uber/nullaway/handlers/CompositeHandler.java

Walkthrough

This PR introduces a new dataflow-analysis hook, onDataflowVisitAssignment, to NullAway's handler interface. The hook enables handlers to observe and refine nullness inference during assignment-node visits in the dataflow graph. OptionalEmptinessHandler implements this hook to recognize and propagate non-null Optional-content facts through assignments originating from Optional.of() and Optional.ofNullable() factory methods. The hook is invoked by the core dataflow engine in AccessPathNullnessPropagation and dispatched to all handlers via CompositeHandler. A new test validates these cases.

Suggested reviewers

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

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 13.64% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: adding tracking for Optional factory methods (Optional.of and Optional.ofNullable) to fix false positives in emptiness detection, which aligns with the core objective of the PR.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ 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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
nullaway/src/main/java/com/uber/nullaway/handlers/CompositeHandler.java (1)

380-398: ⚠️ Potential issue | 🔴 Critical | ⚡ Quick win

Critical: Missing closing brace for onDataflowVisitAssignment method.

The onDataflowVisitAssignment method (starting at line 380) is missing its closing brace. Line 389 only closes the for loop, but the method body itself is never closed before the next method declaration begins at line 390.

🐛 Proposed fix
     for (Handler h : handlers) {
       h.onDataflowVisitAssignment(node, state, apContext, inputs, store, updates);
     }
+  }
+
+  `@Override`
   public boolean isSingleArgNullImpliesFalseMethod(
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@nullaway/src/main/java/com/uber/nullaway/handlers/CompositeHandler.java`
around lines 380 - 398, The method onDataflowVisitAssignment is missing its
closing brace causing the subsequent isSingleArgNullImpliesFalseMethod
declaration to be inside it; add the missing closing brace after the for-loop
that iterates over handlers to properly terminate onDataflowVisitAssignment
(ensure the method signature public void onDataflowVisitAssignment(...) and its
body that calls h.onDataflowVisitAssignment(...) are closed), keep the handlers
iteration unchanged, then recompile/format and run tests to confirm no other
brace mismatches.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Outside diff comments:
In `@nullaway/src/main/java/com/uber/nullaway/handlers/CompositeHandler.java`:
- Around line 380-398: The method onDataflowVisitAssignment is missing its
closing brace causing the subsequent isSingleArgNullImpliesFalseMethod
declaration to be inside it; add the missing closing brace after the for-loop
that iterates over handlers to properly terminate onDataflowVisitAssignment
(ensure the method signature public void onDataflowVisitAssignment(...) and its
body that calls h.onDataflowVisitAssignment(...) are closed), keep the handlers
iteration unchanged, then recompile/format and run tests to confirm no other
brace mismatches.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 9995ab65-cd3e-4a04-9579-f7540813060e

📥 Commits

Reviewing files that changed from the base of the PR and between 7a47f08 and 93f9d5f.

📒 Files selected for processing (2)
  • nullaway/src/main/java/com/uber/nullaway/handlers/CompositeHandler.java
  • nullaway/src/main/java/com/uber/nullaway/handlers/Handler.java

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.

1 participant