Skip to content

Handle negated null checks in multi-argument boolean contracts - #1998

Merged
msridhar merged 2 commits into
uber:masterfrom
vlsi:fix-1995-negated-null-check
Oct 21, 2026
Merged

msridhar merged 2 commits into
uber:masterfrom
vlsi:fix-1995-negated-null-check

Conversation

@vlsi

@vlsi vlsi commented Oct 20, 2026 •

Copy link
Copy Markdown
Collaborator

A null check passed as !(m == null) or !(null == m) to a method with a multi-argument boolean contract such as @Contract("false, false -> fail") made NullAway drop the whole clause, while the same check written as m != null worked. After checkEither(!(m == null), r == null) with a non-null r, dereferencing m was reported as dereferenced expression 'm' is @Nullable. A negation dropped the clause in either argument, including an argument whose value was already known. One-argument fail clauses were not affected, because they are handled by rewriting the CFG around the whole boolean expression.

ContractHandler resolves each boolean argument of such a clause to the target of its null check through NullEqualityVisitor, which matched only EqualToNode and NotEqualNode. The visitor now also matches a ConditionalNotNode by matching its operand with the opposite polarity, so nested negations and parenthesized comparisons resolve as well. The same path serves multi-argument boolean-result clauses such as "true, _ -> true; false, _ -> false", which now accept a negated check too. A negated comparison that is not against null, and a clause with more than one argument of unknown nullness, stay unsupported.

The new tests in ContractsBooleanTests cover each form from the issue, a double negation, and a parenthesized comparison, and fail without the change; negative controls check that !(m != null) and !!(m == null) leave m nullable.

Fixes #1995

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

Summary by CodeRabbit

  • Bug Fixes

    • Improved nullness analysis for negated, reversed, parenthesized, and double-negated null checks.
    • More accurately identifies when boolean contract conditions prove values are non-null.
    • Preserves diagnostics when checks are inconclusive, compare unrelated values, or involve multiple unknown nullable values.
    • Correctly handles two-argument validation contracts, including cases where both conditions are false.
  • Tests

    • Expanded coverage for boolean-expression normalization, nullness contracts, and diagnostic behavior.

A null check passed as `!(m == null)` or `!(null == m)` to a method with a multi-argument boolean contract such as `@Contract("false, false -> fail")` made NullAway drop the whole clause, while the same check written as `m != null` worked. After `checkEither(!(m == null), r == null)` with a non-null `r`, dereferencing `m` was reported as `dereferenced expression 'm' is @Nullable`. A negation dropped the clause in either argument, including an argument whose value was already known. One-argument `fail` clauses were not affected, because they are handled by rewriting the CFG around the whole boolean expression.

`ContractHandler` resolves each boolean argument of such a clause to the target of its null check through `NullEqualityVisitor`, which matched only `EqualToNode` and `NotEqualNode`. The visitor now also matches a `ConditionalNotNode` by matching its operand with the opposite polarity, so nested negations and parenthesized comparisons resolve as well. The same path serves multi-argument boolean-result clauses such as `"true, _ -> true; false, _ -> false"`, which now accept a negated check too. A negated comparison that is not against null, and a clause with more than one argument of unknown nullness, stay unsupported.

The new tests in `ContractsBooleanTests` cover each form from the issue, a double negation, and a parenthesized comparison, and fail without the change; negative controls check that `!(m != null)` and `!!(m == null)` leave `m` nullable.

Fixes uber#1995

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

coderabbitai Bot commented Oct 20, 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: e43e67e1-9496-4f95-a14f-b696573fb7e6
πŸ“₯ Commits

Reviewing files that changed from the base of the PR and between c4a039f and 80c07b9.

πŸ“’ Files selected for processing: 2
  • nullaway/src/main/java/com/uber/nullaway/handlers/contract/ContractHandler.java
  • nullaway/src/test/java/com/uber/nullaway/ContractsBooleanTests.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

The contract handler now recognizes ConditionalNotNode expressions and reverses null-check polarity when interpreting them. Tests cover positive and negative outcomes for checkEither and Validation.identity, including reversed comparisons, double negation, known-null arguments, and unknown nullable arguments.

Suggested reviewers: carterkozak, msridhar

Priority: βž– Normal

Change: Bug fix Β· Severity of issue fixed: Medium

Merge Risk: βšͺ Minimal Β· up to 80c07

The supplied evidence identifies no issue that needs resolution before merging; normal checks still apply.

Pre-merge checks: Passed 4 Β· Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 18 functions across 2 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: handling negated null checks in multi-argument boolean contracts.
Description check Passed The description explains the affected behavior, the implementation, and the tests. It directly relates to the changeset.
Linked Issues check Passed Issue #1995 requires multi-argument boolean contracts to recognize negated null checks in either argument, including reversed comparisons and arguments with known nullness. ContractHandler now handl…
Out of Scope Changes check Passed The reported production change updates null-check resolution for contract analysis. The ContractsBooleanTests additions and contract fixture exercise the behavior and its limits. The available whole…
Full details: Linked Issues check

Explanation

Issue #1995 requires multi-argument boolean contracts to recognize negated null checks in either argument, including reversed comparisons and arguments with known nullness. ContractHandler now handles ConditionalNotNode by matching its operand with opposite polarity. The reported tests cover these cases, nested and parenthesized forms, boolean-result contracts, and unsupported negative cases.

Full details: Out of Scope Changes check

Explanation

The reported production change updates null-check resolution for contract analysis. The ContractsBooleanTests additions and contract fixture exercise the behavior and its limits. The available whole-PR summary identifies no unrelated changes.

✨ 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 20, 2026 •

Copy link
Copy Markdown

Codecov Report

βœ… All modified and coverable lines are covered by tests.
βœ… Project coverage is 88.15%. Comparing base (486dff9) to head (80c07b9).

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #1998      +/-   ##
============================================
+ Coverage     88.11%   88.15%   +0.04%     
  Complexity       38       38              
============================================
  Files           117      117              
  Lines         11944    11946       +2     
  Branches       2447     2448       +1     
============================================
+ Hits          10524    10531       +7     
+ Misses          644      641       -3     
+ Partials        776      774       -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 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

A bit strange to write code this way but might as well handle it πŸ™‚

@msridhar
msridhar enabled auto-merge (squash) October 21, 2026 01:29
@msridhar
msridhar merged commit b2ef20b into uber:master Oct 21, 2026
20 checks passed
msridhar pushed a commit that referenced this pull request Oct 22, 2026
A boolean `@Contract` clause lost the null check passed to it when the argument was wrapped in a cast. After `checkEither((boolean) (m != null), r == null)` under `@Contract("false, false -> fail")`, with a non-null `r`, NullAway still reported `dereferenced expression 'm' is @Nullable` on `m.toString()`, though the same call without the cast reports nothing. A cast on the other argument, `checkEither(m != null, (boolean) (r == null))`, lost the fact the same way, and so did a cast under a `true, _ -> true` clause. This is a variation of #1993; #1995 reported the neighboring case of a negated null check, which #1998 fixed.

`ContractHandler` finds the null check in a boolean argument with `NullEqualityVisitor`, which matched `==`, `!=`, and `!` and returned nothing for a cast. The visitor now looks through a cast node with the same polarity, so casts, negations, and parentheses compose in any nesting: `!((boolean) (m == null))` and `(boolean) !(m == null)` both count as `m != null`. A `fail` clause with a single boolean antecedent, such as `@Contract("false -> fail")`, was not affected: it is handled by inserting a conditional throw on the argument, and the dataflow transfer for a cast already passes its operand's then and else stores through.

New tests in `ContractsBooleanTests` cover the cast on either argument, both nestings with a negation, and a `true, _ -> true` clause; each failed without the change. A cast around `m == null` still reports, and a cast under a single-argument `false -> fail` clause is covered as a guard.

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

πŸ€– Generated with [Claude Code](https://claude.com/claude-code)


<!-- This is an auto-generated comment: release notes by coderabbit.ai -->

<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

- **Bug Fixes**
  - Improved nullability analysis for boolean null checks wrapped in type casts.
  - Contract-based checks now correctly preserve and infer nullability across casted and negated expressions.
  - Added support for casted checks in one- and two-argument failure contracts and true-implies-true contracts.

- **Tests**
  - Added regression coverage for casted, negated, and non-null null-check scenarios.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
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.

Multi-argument @Contract("false, false -> fail") ignores a null check written as !(x == null)

2 participants