dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.22k stars 1.57k forks source link

Potentially spurious static type computation in the Analyzer #56963

Open chloestefantsova opened 1 week ago

chloestefantsova commented 1 week ago

Consider the following code:

extension on int? {
  int? operator+(int? other) {
    if (this != null && other != null) {
      return this! + other;
    } else {
      return null;
    }
  }
}

int test(int? x) {
  return ++x ?? 10;
}

The expected static type of ++x is int?, given the return type of operator+. However, the Analyzer reports the following warning:

warning - qwerty.dart:12:17 - The left operand can't be null, so the right operand is never executed. Try removing the operator and the right operand. - dead_null_aware_expression
chloestefantsova commented 1 week ago

/cc @eernstg @sgrekhov

adenes commented 6 days ago

I hope you don't mind me chiming in. I did some debugging and it seems this is where the nullability from int? gets lost: https://github.com/dart-lang/sdk/blob/main/pkg/analyzer/lib/src/dart/resolver/prefix_expression_resolver.dart#L231-L232

bwilkerson commented 6 days ago

I hope you don't mind me chiming in.

No, we don't mind. We welcome all contributions. Thanks for digging into this and letting us know what you found!

adenes commented 1 day ago

https://dart-review.googlesource.com/c/sdk/+/392905