dart-lint icon indicating copy to clipboard operation
dart-lint copied to clipboard

Need explanation: why missing_return is set to warning

Open haashem opened this issue 4 years ago • 4 comments

If I forgot to return value from method complier shows warning instead of error! why it's a default behavior?

Screen Shot 2020-05-17 at 4 07 46 PM

haashem avatar May 17 '20 11:05 haashem

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

passsy avatar May 18 '20 08:05 passsy

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?

haashem avatar May 18 '20 10:05 haashem

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.

passsy avatar May 18 '20 11:05 passsy

Thanks for clarifications. should I create a PR for 'missing_return: error'?

haashem avatar May 19 '20 10:05 haashem