Open feinstein opened 4 weeks ago
The compiler doesn't recognize that x?.y != null
implies that x
is not null
.
Only comparisons directly against it x
can promote x
.
There are existing issues suggesting such indirect implications, but they're not always as clear-cut as directed checks out the variable.
In this case x?.y != null
can promote x
to non-null
, but x?.y == null
does not imply that x
is null
.
Similar to #1224.
Doesn't this work for you?
final expirationDate = transactions.lastOrNull?.expirationDate;
if (expirationDate == null) {
return false;
}
final parsedExpirationDate = DateTime.tryParse(expirationDate);
In this case x?.y != null can promote x to non- null, but x?.y == null does not imply that x is null.
@lrhn I agree that x?.y == null
doesn't imply that x is null, but in this case the compiler could be able to have some more sofisticated logic to promote it, as if the if-check is not null, then x could not be null.
Do you want me to leave this as a request or close it?
@mateusfccp I made it work, thanks, my goal with this snippet was more to showcase my point.
Consider this code:
If I remove the
!
, I get an analyser error saying I can't uselastTransaction
because it can benull
. I believe the analyser should be able to conclude thatlastTransaction
can't benull
, because it's a final local variable, and the?.
in theif
check would ensure there's no valid code path for it to still benull
.