dart-lint
dart-lint copied to clipboard
Need explanation: why missing_return is set to warning
If I forgot to return value from method complier shows warning instead of error! why it's a default behavior?

The code works fine. I was very confused when I saw it at first.
void main() {
final name = getName();
print(name); // null
}
String getName() {
}
Methods in dart automatically return null
when return is missing. See https://github.com/dart-lang/sdk/issues/13373
This implicit behaviour will also be true
with nnbd when you work with nullable types. For non-nullable types you'll see a compile error.
String? getName() {
// compiler implicitly injects return null;
}
String getName() {
// Error, null isn't allowed
}
I don't think anyone wants to rely on the implicit compiler behaviour and a explicit return will make dart easier to read. Therefore I'll change the severity
to error.
analyzer:
errors:
missing_return: error
I read the link, but they have not mentioned the technical reason behind implicit return type. do you know?
there is another issue in analysis_options.yaml
:
missing_required_param: warning
but it should be
missing_required_param : error
any reason behind this?
Every functions in dart has a return value. Even void
functions - when casted to Function
- return null
. With that logic it's a small step to default to null
for all functions.
void main() {
// Error: This expression has type 'void' and can't be used.
// print(doSomething());
// Workaround to get the result of a void function
Function f = doSomething;
final result = f();
print(result);
}
void doSomething() {
}
From my feeling missing_required_param
is better off as warning
. Mostly because you don't want errors in your code because some library incorrectly uses @required
. missing_return
is different as it doesn't interfere with other libraries.
In the end, all hints/warnings/errors reported by the analyzer are the same thing, colored differently in you IDE.
Thanks for clarifications. should I create a PR for 'missing_return: error'?