Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Need explanation: why missing_return is set to warning #10

Open
haashem opened this issue May 17, 2020 · 4 comments
Open

Need explanation: why missing_return is set to warning #10

haashem opened this issue May 17, 2020 · 4 comments

Comments

@haashem
Copy link

haashem commented May 17, 2020

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

@passsy
Copy link
Owner

passsy commented May 18, 2020

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 dart-lang/sdk#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

@haashem
Copy link
Author

haashem commented May 18, 2020

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?

@passsy
Copy link
Owner

passsy commented May 18, 2020

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.

@haashem
Copy link
Author

haashem commented May 19, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants