I have a filed bytes
in class in Kotlin:
var bytes: ByteArray? = null
get() = when {
field != null -> Arrays.copyOf(field, field!!.size)
else -> field
}
set(value) {
field = when {
value != null -> Arrays.copyOf(value, value.size)
else -> null
}
}
Why in 3rd line there must be an !!
operator for field
?
Without !!
Idea shows:
Smart cast to 'ByteArray' is impossible, because 'field' is a mutable property that could have been changed by this time
Condition (field != null)
ensures that field is null in if body (right side). Or not? Or it can be reassigned to null meanwhile? How is this possible?
With above code FindBugs warns:
Redundant nullcheck of com.xy.Some.bytes which is known to be null in com.xy.Some.getBytes()
This method contains a redundant check of a known null value against the constant null.
http://findbugs.sourceforge.net/bugDescriptions.html#RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE