dart-lang / dart-lang/language

False analyzer warnings when using nullable extensions

Open
#3,645 2 comments 0 reactions 0 assignees View on GitHub
request
Dominant language
TeX
Stars
2.9k
Forks
239
Avg merge
2d 18h
Merged PRs (30d)
14

Description

Consider the following example:
```dart
void main() {
final _A? e = _Aaa();
switch (e) {
case null: // using Null() works as expected
e.name; // should be able to call .name

case _Aa():
print(e.name);
}

// using is Null works as expected
if (e == null) {
e.name; // should be able to call .name
}
}

sealed class _A {}

sealed class _Aa extends _A {}

final class _Aaa extends _Aa {}

extension on _Aa? {
String get name {
return switch (this) {
null => 'null',
_Aaa() => 'a:a:a',
};
}
}
```
It seems when explicitly checking for `null` the analyzer still treats the variable as `T?`, making it impossible to use an extension method defined on a more specific type and null.

A solution is to use an `is Null` check, however, in that case the `type_check_with_null` lint triggers and prompts the user to use a `== null` check instead.

Contributor guide

Open the contributing guide

Research direction

Start with the Dart example in the issue and reproduce the analyzer warning for the `switch (e)` null case, then compare it with the `e == null` and `is Null` checks. Trace the analyzer's type promotion for nullable extensions; done means the explicit null case permits the extension and does not require the conflicting workaround lint.

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
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.