Open
Description
$ dart --version
Dart SDK version: 3.6.0 (stable) (Thu Dec 5 07:46:24 2024 -0800) on "macos_x64"
In the following snippet, VSCode reports the error at the specified location.
mixin Equatable {}
sealed class S {}
class A extends S with Equatable {}
class B extends S with Equatable {}
S f1(bool b) {
return b ? A() : B();
// ^^^^^^^^^^^^^ A value of type 'Object' can't be returned from the function 'f1' because it has a return type of 'S'
}
// control flow analysis can figure it out, no error here:
S f2(bool b) {
S s;
if (b) s = A();
else s = B();
return s;
}
However dart analyze
finds no issues:
$ dart analyze lib/lub.dart
Analyzing lub.dart... 0.7s
No issues found!
Removing the with Equatable
clauses fixes the warning.
Interestingly, adding a top type to S
also seems to fix the warning:
mixin Equatable {}
sealed class _Top {}
sealed class S extends _Top {}
class A extends S with Equatable {}
class B extends S with Equatable {}
S f1(bool b) {
return b ? A() : B();
}
S f2(bool b) {
S s;
if (b) s = A();
else s = B();
return s;
}