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.09k stars 1.56k forks source link

[extension types/analyzer] `The receiver can't be null` false negative on `extension type` typed values? #56759

Open modulovalue opened 2 hours ago

modulovalue commented 2 hours ago

Consider:

void main() {
  const Foo a = Foo(0);
  const int b = 0;

  a?.toString();
// The receiver can't be null, so the null-aware operator '?.' is unnecessary.
// v
  b?.toString();
}

extension type const Foo(int i) {}

b?.toString(); causes the analyzer to emit a The receiver can't be null, so the null-aware operator '?.' is unnecessary. diagnostic, but it looks to me like a?.toString(); could also be annotated with a similar diagnostic.

dart-github-bot commented 2 hours ago

Summary: The analyzer incorrectly fails to detect that a null-aware operator is unnecessary for an extension type (Foo) when it should, as it does for a regular type (int). This suggests a potential false negative in the analyzer's null-aware operator analysis for extension types.