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

[NNBD] explicit_returns #2352

Closed
modulovalue opened this issue Nov 29, 2020 · 8 comments
Closed

[NNBD] explicit_returns #2352

modulovalue opened this issue Nov 29, 2020 · 8 comments
Labels
lint-request type-enhancement A request for a change that isn't a bug

Comments

@modulovalue
Copy link
Contributor

Dart interprets not returning anything, as returning a null.

A lint to warn the user to explicitly return null when a null would be inferred due to a missing return statement would be very helpful.

i.e. such that code like:

int? foo() {

}
// or this
int? foo() {
  if (a is! ...) {
    throw Exception();
  } else {
    if (b is! ...) {
      throw Exception();
    } else {
      if (c is ...) {

        if (...) {
          throw Exception();
        }

        if (...) {
          throw Exception();
        }

        if (...) {
          throw Exception();
        }

        return 0;
      } 
    }
  }
}

would emit a warning on the return type that not all code paths have a return statement.

@modulovalue modulovalue added type-enhancement A request for a change that isn't a bug lint-request labels Nov 29, 2020
@bwilkerson
Copy link
Member

Just to confirm, I think you're asking for a lint to enforce the style preference of always having an explicit return, even when the returned value is null.

@modulovalue
Copy link
Contributor Author

Yes, that is correct.

@marcglasberg
Copy link

marcglasberg commented Sep 5, 2021

Please, my team also needs this. It's just now very easy to forget a return, not because you want to return null, but because you forgot to return something different than null in some code branch, and it just assumes it's null and you are on your own, the compiler doesn't care.

We've been having a lot of bugs relating to this.

@modulovalue
Copy link
Contributor Author

This issue also comes up in a similar form when implicit control flow jumps via Never are being introduced.

Invoking e.g. a function that returns Never will "automatically throw".

void foo() {
  bar();
  // Anything below bar will never be executed, but there's no throw/return/break/... anywhere which is confusing given how the rest of Dart works
  print("baz");
}

Never bar() {...}

Forcing users to throw all returned Never would be great because it would match the expectations of explicit control flow that users are used to in Dart (as opposed to e.g. Rust where the last statement returns implicitly).

void foo() {
//vvvvv explicit control flow
  throw bar();
  print("baz");
}

Never bar() {...}

So, maybe this issue, and the Never issue mentioned above could be solved by a new explicit_control_flow lint?

@srawlins
Copy link
Member

srawlins commented Feb 7, 2023

This is implemented as body_might_complete_normally_nullable.

@srawlins
Copy link
Member

srawlins commented Feb 7, 2023

Forcing users to throw all returned Never would be great because it would match the expectations of explicit control flow that users are used to in Dart

I'm not against this idea; there would be definite benefits to being explicit. If you want to pursue this, please open a new request.

@srawlins srawlins closed this as completed Feb 7, 2023
@modulovalue
Copy link
Contributor Author

This is implemented as body_might_complete_normally_nullable.

Thank you @srawlins, that obviously fixes this issue, I failed to consider that.

@modulovalue
Copy link
Contributor Author

@srawlins See: #4054

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lint-request type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

4 participants