Repository navigation
Recognize boolean constants behind a cast in EnsuresNonNullIf and stream filters - #2011
Conversation
…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)
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
WalkthroughStream nullability propagation and Suggested reviewers: Priority: ⬇️ Low Change: Bug fix Merge Risk: ⚪ Minimal · up to
|
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
A
return (boolean) (false);was treated as a value that may be true, so NullAway reported false positives where the same code withreturn false;compiles cleanly. An@EnsuresNonNullIf("item")method that returnstruewhenitem != nulland(boolean) (false)otherwise reportedMethod is annotated with @EnsuresNonNullIf but does not ensure fields [item], and a stream or RxJavafiltercallback that returns(boolean) (false)whenc.f == nullreporteddereferenced expression 'c.f' is @Nullablein the followingmap. This is a variation of #1993: #2002 taughtEnsuresNonNullIfHandlerandStreamNullabilityPropagator.canBooleanExpressionEvalToTrueto see a boolean literal through parentheses, and a cast still hid it.Both sites now read the value with Error Prone's
ASTHelpers.constValue, whichContractHandleralready uses for contract arguments. It strips parentheses and returns the value javac folded for a constant expression, so a cast of a literal and astatic final booleanconstant 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) falseis not a constant expression and stays unrecognized.Caller sees:
Method is annotated with @EnsuresNonNullIf but does not ensure fieldswarning on a return of a boolean constant expression that is the opposite of the annotation'sresult.dereferenced expression ... is @Nullablewarning after a stream or RxJavafiltercallback whose null-check branch returns a constantfalse.The new tests in
EnsuresNonNullIfTestsandFrameworkTestscover(boolean) (false)and afalseconstant field at each site, and the cast in an RxJavafilter; each failed on master with the false positive. Controls that return(boolean) (true)or a non-final field that holdsfalsestill report.No other handler matches a boolean
LiteralTreethis way.AccessPath.buildAccessPathRecursivematches 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
truevalues are not treated as successful nullability refinements.Tests