NullAway: False positive with field nullability stored in local variable
In this case, a @Nullable field is stored to a local variable. When the field is used in a null condition an error occurs, but if the variable is used then it does not. It seems the data flow analysis does not realize they are the same.
error: [NullAway] returning @Nullable expression from method with @NonNull return type
return (writer == null) ? CacheWriter.disabledWriter() : castedWriter;
^
<K1 extends K, V1 extends V> CacheWriter<K1, V1> getCacheWriter() {
@SuppressWarnings("unchecked")
CacheWriter<K1, V1> castedWriter = (CacheWriter<K1, V1>) writer;
return (writer == null) ? CacheWriter.disabledWriter() : castedWriter;
}
About this issue
- Original URL
- State: open
- Created 9 years ago
- Comments: 20
Hitting this a reasonable amount as well for Chrome code. Same examples as above. E.g.:
Our C++ colleagues at Google have done some work to handle variables like the
definitelyNotNullexample above. My understanding is that they’re currently using satisfiability checking but that they are considering alternative approaches that might perform better, including essentially “inlining”potentiallyNull != nullin place of usages ofdefinitelyNotNull(potentially inlining further recursively). (I’m sure there would be caveats around cases in whichpotentiallyNullmight change value, for example.) I’m told that one approach (similar but not identical to “inlining”) is being demoed in https://github.com/llvm/llvm-project/pull/82950.Yes, though the codegen is only for minimizing fields and the logic is in the base class. I would expect inlining to remove the downcasts, so it would be free. But the project has exceeded its complexity budget and this would add obscurity when already challenging. But this is a good trick to know if I decide to introduce NullAway to a work project. Thanks!