Repository navigation
Handle negated null checks in multi-argument boolean contracts - #1998
Conversation
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)
|
No actionable comments were generated in the recent review. π βΉοΈ Recent review info
WalkthroughThe contract handler now recognizes Suggested reviewers: Priority: β Normal Change: Bug fix Β· Severity of issue fixed: Medium Merge Risk: βͺ Minimal Β· up to
|
Codecov Reportβ
All modified and coverable lines are covered by tests. 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. π New features to boost your workflow:
|
msridhar
left a comment
There was a problem hiding this comment.
A bit strange to write code this way but might as well handle it π
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 -->
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 asm != nullworked. AftercheckEither(!(m == null), r == null)with a non-nullr, dereferencingmwas reported asdereferenced expression 'm' is @Nullable. A negation dropped the clause in either argument, including an argument whose value was already known. One-argumentfailclauses were not affected, because they are handled by rewriting the CFG around the whole boolean expression.ContractHandlerresolves each boolean argument of such a clause to the target of its null check throughNullEqualityVisitor, which matched onlyEqualToNodeandNotEqualNode. The visitor now also matches aConditionalNotNodeby 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
ContractsBooleanTestscover 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)leavemnullable.Fixes #1995
Assisted-by: Claude Code (claude-opus-5-5)
Summary by CodeRabbit
Bug Fixes
Tests