Skip to content

Better support for some contracts with boolean argument constraints#1447

Merged
msridhar merged 7 commits intomasterfrom
better-contract-support-1232
Jan 26, 2026
Merged

Better support for some contracts with boolean argument constraints#1447
msridhar merged 7 commits intomasterfrom
better-contract-support-1232

Conversation

@msridhar
Copy link
Copy Markdown
Collaborator

@msridhar msridhar commented Jan 26, 2026

Fixes #1232

We now support contracts like @Contract("false -> !null"). This turned out to be easier than I thought (Codex helped :-) ). The support only detects cases when the literal false or true is passed at the call site. And, we currently cannot verify these contracts; we can only use them at call sites.

Summary by CodeRabbit

  • Improvements

    • Contract analysis now recognizes compile-time boolean constants and short-circuits inference when an argument’s constant satisfies or contradicts a boolean contract constraint.
  • Tests

    • Added tests validating boolean-only, multi-argument boolean, and mixed boolean/null contracts to ensure correct nullability propagation and diagnostics.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jan 26, 2026

Walkthrough

Adds handling for compile-time constant boolean antecedents in contract analysis. ContractHandler now, during onDataflowVisitMethodInvocation, inspects boolean-literal antecedents and checks the corresponding invocation argument via ASTHelpers.constValue on an ExpressionTree: if the argument is a compile-time boolean matching the antecedent the clause continues, otherwise the clause is marked unsupported and processing breaks. The change adds an import for com.sun.source.tree.ExpressionTree and introduces an early short-circuit path for boolean-constraint handling. Three tests exercising boolean and mixed boolean/null contracts were added.

Possibly related PRs

Suggested reviewers

  • yuxincs
  • lazaroclapp
🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ 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%. 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 support for contracts with boolean argument constraints, which is the primary focus of the implementation changes.
Linked Issues check ✅ Passed The PR successfully addresses issue #1232 by implementing support for @Contract annotations with boolean literal constraints (e.g., 'false -> !null') at call sites.
Out of Scope Changes check ✅ Passed All changes are directly related to implementing boolean constraint handling in contracts; no unrelated modifications are present.

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

✨ Finishing touches
  • 📝 Generate docstrings

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.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Fix all issues with AI agents
In `@nullaway/src/test/java/com/uber/nullaway/ContractsTests.java`:
- Around line 777-779: The test currently calls nonNullWhenPassedFalse(b || !b)
with a "false -> !null" contract but b || !b is always true, so the warning is a
true positive; to make this a false-positive test change the call to
nonNullWhenPassedTrue(b || !b) (the method with the "true -> !null" contract) so
the contract should guarantee non-null at runtime but NullAway still emits a
warning, or alternatively update the comment and the expected diagnostic to
reflect that this is a true positive if you intend to keep calling
nonNullWhenPassedFalse.

Comment thread nullaway/src/test/java/com/uber/nullaway/ContractsTests.java Outdated
@codecov
Copy link
Copy Markdown

codecov Bot commented Jan 26, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.41%. Comparing base (4c619e8) to head (d349d69).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #1447      +/-   ##
============================================
+ Coverage     88.40%   88.41%   +0.01%     
- Complexity     2712     2714       +2     
============================================
  Files            99       99              
  Lines          9007     9015       +8     
  Branches       1799     1801       +2     
============================================
+ Hits           7963     7971       +8     
  Misses          517      517              
  Partials        527      527              

☔ View full report in Codecov by Sentry.
📢 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 requested a review from yuxincs January 26, 2026 06:00
@msridhar msridhar merged commit b27f13d into master Jan 26, 2026
12 checks passed
@msridhar msridhar deleted the better-contract-support-1232 branch January 26, 2026 15:11
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.

@Contract("false -> !null") not honored

2 participants