Skip to content

Recognize boolean constants behind a cast in EnsuresNonNullIf and stream filters - #2011

Merged
msridhar merged 2 commits into
uber:masterfrom
vlsi:cast-boolean-literal
Oct 22, 2026
Merged

msridhar merged 2 commits into
uber:masterfrom
vlsi:cast-boolean-literal

Conversation

@vlsi

@vlsi vlsi commented Oct 21, 2026 •

Copy link
Copy Markdown
Collaborator

A return (boolean) (false); was treated as a value that may be true, so NullAway reported false positives where the same code with return false; compiles cleanly. An @EnsuresNonNullIf("item") method that returns true when item != null and (boolean) (false) otherwise reported Method is annotated with @EnsuresNonNullIf but does not ensure fields [item], and a stream or RxJava filter callback that returns (boolean) (false) when c.f == null reported dereferenced expression 'c.f' is @Nullable in the following map. This is a variation of #1993: #2002 taught EnsuresNonNullIfHandler and StreamNullabilityPropagator.canBooleanExpressionEvalToTrue to see a boolean literal through parentheses, and a cast still hid it.

Both sites now read the value with Error Prone's ASTHelpers.constValue, which ContractHandler already uses for contract arguments. It strips parentheses and returns the value javac folded for a constant expression, so a cast of a literal and a static final boolean constant field are recognized as well. Anything that is not a compile-time constant is still treated as possibly true. A boxing cast such as (Boolean) false is not a constant expression and stays unrecognized.

Caller sees:

  • No Method is annotated with @EnsuresNonNullIf but does not ensure fields warning on a return of a boolean constant expression that is the opposite of the annotation's result.
  • No dereferenced expression ... is @Nullable warning after a stream or RxJava filter callback whose null-check branch returns a constant false.

The new tests in EnsuresNonNullIfTests and FrameworkTests cover (boolean) (false) and a false constant field at each site, and the cast in an RxJava filter; each failed on master with the false positive. Controls that return (boolean) (true) or a non-final field that holds false still report.

No other handler matches a boolean LiteralTree this way. AccessPath.buildAccessPathRecursive matches constant method-call arguments, including boolean literals, by tree kind without stripping casts; that is a different defect, about access paths, and is not part of this change.

Assisted-by: Claude Code (claude-opus-5-5)

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes

    • Nullability analysis now recognizes cast boolean values and final boolean constants in conditional contracts and stream or reactive filters.
    • Mutable boolean fields and constant true values are not treated as successful nullability refinements.
    • Analysis no longer errors on non-Boolean literals.
  • Tests

    • Added coverage for conditional contracts and stream and reactive filter behavior with constant expressions.

…eam filters

A `return (boolean) (false);` was treated as a value that may be true, so NullAway reported false positives where the same code with `return false;` compiles cleanly. An `@EnsuresNonNullIf("item")` method that returns `true` when `item != null` and `(boolean) (false)` otherwise reported `Method is annotated with @EnsuresNonNullIf but does not ensure fields [item]`, and a stream or RxJava `filter` callback that returns `(boolean) (false)` when `c.f == null` reported `dereferenced expression 'c.f' is @Nullable` in the following `map`. This is a variation of uber#1993: uber#2002 taught `EnsuresNonNullIfHandler` and `StreamNullabilityPropagator.canBooleanExpressionEvalToTrue` to see a boolean literal through parentheses, and a cast still hid it.

Both sites now read the value with Error Prone's `ASTHelpers.constValue`, which `ContractHandler` already uses for contract arguments. It strips parentheses and returns the value javac folded for a constant expression, so a cast of a literal and a `static final boolean` constant field are recognized as well. Anything that is not a compile-time constant is still treated as possibly true. A boxing cast such as `(Boolean) false` is not a constant expression and stays unrecognized.

Caller sees:
- No `Method is annotated with @EnsuresNonNullIf but does not ensure fields` warning on a return of a boolean constant expression that is the opposite of the annotation's `result`.
- No `dereferenced expression ... is @Nullable` warning after a stream or RxJava `filter` callback whose null-check branch returns a constant `false`.

The new tests in `EnsuresNonNullIfTests` and `FrameworkTests` cover `(boolean) (false)` and a `false` constant field at each site, and the cast in an RxJava `filter`; each failed on master with the false positive. Controls that return `(boolean) (true)` or a non-final field that holds `false` still report.

No other handler matches a boolean `LiteralTree` this way. `AccessPath.buildAccessPathRecursive` matches constant method-call arguments, including boolean literals, by tree kind without stripping casts; that is a different defect, about access paths, and is not part of this change.

Assisted-by: Claude Code (claude-opus-5-5)
@coderabbitai

coderabbitai Bot commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: uber/NullAway/.coderabbit.yaml
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 610cfa40-e5f0-485e-b582-792e11a61f40
📥 Commits

Reviewing files that changed from the base of the PR and between 7bd4925 and 8d582b3.

📒 Files selected for processing: 4
  • nullaway/src/main/java/com/uber/nullaway/handlers/StreamNullabilityPropagator.java
  • nullaway/src/main/java/com/uber/nullaway/handlers/contract/fieldcontract/EnsuresNonNullIfHandler.java
  • nullaway/src/test/java/com/uber/nullaway/EnsuresNonNullIfTests.java
  • nullaway/src/test/java/com/uber/nullaway/FrameworkTests.java

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 5 remain after this review.

Walkthrough

Stream nullability propagation and @EnsuresNonNullIf validation now use constant evaluation to recognize boolean expressions beyond direct literals. Added tests cover casts, final and mutable boolean fields, stream filters, and Observable filters.

Suggested reviewers: msridhar, mauricioaniche

Priority: ⬇️ Low

Change: Bug fix

Merge Risk: ⚪ Minimal · up to 8d582

The change addresses the stated false positives without an identified merge-blocking risk. It is ready for normal checks.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 15.79% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 19 functions across 4 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title clearly and concisely describes the main change: recognizing boolean constants behind casts in EnsuresNonNullIf handling and stream filters.
Description check Passed The description is directly related to the changeset. It explains the false positives, the use of ASTHelpers.constValue, supported constant forms, limitations, and test coverage.
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.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

@codecov

codecov Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.78%. Comparing base (ad67857) to head (8d582b3).

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #2011      +/-   ##
============================================
+ Coverage     88.76%   88.78%   +0.01%     
  Complexity       38       38              
============================================
  Files           118      118              
  Lines         12551    12543       -8     
  Branches       2543     2540       -3     
============================================
- Hits          11141    11136       -5     
+ Misses          642      641       -1     
+ Partials        768      766       -2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@msridhar
msridhar enabled auto-merge (squash) October 22, 2026 03:48
@msridhar
msridhar merged commit cc36b85 into uber:master Oct 22, 2026
19 of 20 checks passed
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.

2 participants