Flow analysis doesn't recognize that null check patterns always match a non-nullable type
- Dominant language
- Dart
- Stars
- 11.3k
- Forks
- 1.9k
- PR merge metrics
- PR metrics pending
Description
Flow analysis currently (as of cb4c9c74de5169a61febc94b0e8a5983791e826e) fails to recognize that if the matched value type is non-nullable, then a null check pattern (e.g. `_?`) or the equivalent relational pattern (`!= null`), will always match.
As a result, flow analysis can't recognize that a switch statement like this one is trivially exhaustive:
```dart
bool f(int i) { // (1)
switch (i) {
case var _?:
return i.isEven;
}
}
main() {
print(f(0));
print(f(1));
}
```
And so an error is issued at (1), saying "The body might complete normally, causing 'null' to be returned, but the return type, 'bool', is a potentially non-nullable type."
If flow analysis were slightly smarter, it would see that no error needs to be issued at (1), because `var _?` always matches the non-nullable type `int`, so there would be no error.
Contributor guide
Research direction
Start by reproducing the reported Dart switch example and inspect the flow-analysis handling for null-check patterns and equivalent `!= null` relational patterns. Done means a non-nullable matched type is recognized as always matching, the switch is treated as exhaustive, and no potentially-nullable return error is reported.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- dart
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100