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

No analyzer hint for function returning nullable non-Null type which implicitly falls off the edge #46656

Closed
srawlins opened this issue Jul 19, 2021 · 11 comments
Assignees
Labels
analyzer-warning Issues with the analyzer's Warning codes area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug

Comments

@srawlins
Copy link
Member

Before null safety, the analyzer would report a Hint here:

int f() {}

It was perfectly legal to implicitly return null in this function. But a Hint was added because it looks highly suspicious (and the linter package was just a dream).

In the null safe type system, the analyzer does not report a Hint here:

int? f() {}

Again, this is perfectly legal because of an implicit return when falling off the edge of a function body. But again, it is highly suspicious. I think there should be a Hint.

If we decide that the second function is less suspicious than the first (it implicitly returns null for an explicitly nullable type, rather than an implicitly nullable type), then I'd say it still deserves a Hint; a Lint at the least.

@srawlins srawlins added area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. analyzer-warning Issues with the analyzer's Warning codes P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug labels Jul 19, 2021
@srawlins
Copy link
Member Author

CC @bwilkerson @pq who I hope have some thoughts. 😄 I know you're also acutely aware of the linter's coverage for null safety concepts.

@bwilkerson
Copy link
Member

I remember having a discussion in which we decided that the hint (missing_return) was not needed under null safety when the return type was non-nullable because it became a compile time error. I don't remember explicitly deciding that we shouldn't have it in the nullable case, so it's possible that it was disabled too broadly.

I would tend to agree that it's better to have an explicit return because it indicates that the author actually considered what would be returned if the end of the function was reached. As for the hint vs. lint question, I can see arguments for both.

It might be interesting to re-enable the hint and analyze a large body of code to see whether people have started to rely on being able to omit the return at the end. It might also be interesting to get an opinion from the language team.

@lrhn

@lrhn
Copy link
Member

lrhn commented Jul 26, 2021

I would personally prefer to force you to write return in any non-voidy non-generator function (any return type except FutureOrn<void>, or Future<voidy> for async functions).

We made exceptions for Null and dynamic, but mostly for backwards compatibility reasons. I'd love to get rid of those (at least the Null exception)

Can't speak for the entire language team, I'm definitely stricter than average.

... and yet, the same day I wrote this, I ended up designing an API where I returned Future<void>? (optionally a future, but not FutureOr<void>, because I wanted to use ?? on the null result). And while using that, I very much wanted to not have to write return null; in the no-future-returned case. So, just maybe, allowing not returning anything from a nullable function is reasonable in some cases. (Like, my cases, not yours! 😈)

@davidmorgan
Copy link
Contributor

+1 for the hint; this looks like an accidental regression to me.

It was noticed by an internal customer, they were about to create style guidance around it but I suggested we first check if there is a possibility of fixing it :)

@srawlins
Copy link
Member Author

I'd say important to do it sooner rather than later, as the pace of migrating to null safety is increasing internally; implementing this check earlier means less clean-up.

@eernstg
Copy link
Member

eernstg commented Oct 13, 2021

About the idea that this situation should be flagged: 👍

Here is a situation which is related: int? f() { return; } is actually a compile-time error, so we don't allow the null to be implicit in a return statement (because the return type in this case isn't void, dynamic, or Null).

I believe the only reason why it isn't an error to return null silently by reaching the end is that this situation wasn't well characterized in pre-null-safety Dart (we did not have a precise control-flow analysis).

So it would be nice to have a lint (or hint) now, especially because it seems to be non-breaking. Then we could make it an error later on, for consistency.

@jefflim-google
Copy link

+1, would love to see this hint or even error be implemented.

@srawlins srawlins self-assigned this Nov 18, 2021
copybara-service bot pushed a commit that referenced this issue Nov 30, 2021
Related to: #46656

Change-Id: I5c015e9827cc7b7ac26f198371709c124b6c28ea
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/221565
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Samuel Rawlins <srawlins@google.com>
@srawlins
Copy link
Member Author

This turns out to have a lot of existing violations arleady in Flutter and even the Dart SDK repo. :( I might need to implement this as an opt-in Lint or a newly-named default opt-in Hint (warning). I found in the analyzer codebase a common pattern:

Foo? getFoo(List gadgets) {
  for (var gadget in gadgets) {
    if (...) return gadget.foo;
  }
}

The fix for such methods is to add a return null; at the end. This felt unfortunate, but does still make the code more clear. In any case, we might face resistance to this as a default opt-in Hint, if it just means making implicit return null statements explicit.

@lrhn
Copy link
Member

lrhn commented Nov 30, 2021

I don't personally think having to add a return null; is unfortunate. If anything, I'm very sorry it's not already required. It really should have been since Dart 2.0. (Only void functions can omit returns, this is omitting a return!)

@bwilkerson
Copy link
Member

Given the potential impact on the ecosystem, I believe that we should initially implement it as a lint, add it to the recommended / core lint set at some later point, and consider making it a warning or error in some future language version.

@pq
Copy link
Member

pq commented Nov 30, 2021

Given the potential impact on the ecosystem, I believe that we should initially implement it as a lint

+1.

copybara-service bot pushed a commit that referenced this issue Dec 8, 2021
Bug: #46656
Change-Id: I8a37d4b08ea678b7d021f173448d6770b04f1d34
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/222384
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Samuel Rawlins <srawlins@google.com>
copybara-service bot pushed a commit that referenced this issue Dec 8, 2021
Bug: #46656
Change-Id: I9ea7d4949f8384433dfd8a6cca79e957a4f6e05c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/222327
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Samuel Rawlins <srawlins@google.com>
copybara-service bot pushed a commit that referenced this issue Dec 8, 2021
Bug: #46656
Change-Id: Ifbc498763fdc38440f12f394a5981c4d70fd83bf
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/222380
Reviewed-by: Paul Berry <paulberry@google.com>
Commit-Queue: Samuel Rawlins <srawlins@google.com>
copybara-service bot pushed a commit that referenced this issue Dec 14, 2021
…llable return types

Bug: #46656
Change-Id: I2e0069460f9f63b7d985169656273a1c5ffd76d1
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/223540
Auto-Submit: Samuel Rawlins <srawlins@google.com>
Reviewed-by: Johnni Winther <johnniwinther@google.com>
Commit-Queue: Samuel Rawlins <srawlins@google.com>
copybara-service bot pushed a commit that referenced this issue Dec 16, 2021
Bug: #46656
Change-Id: I130903d6c009d60bcf3c1fee2378abb96c28714f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/224381
Reviewed-by: Ben Konyi <bkonyi@google.com>
Commit-Queue: Ben Konyi <bkonyi@google.com>
copybara-service bot pushed a commit that referenced this issue Dec 22, 2021
This reverts commit 93bcdf0.

Reason for revert: Broke google3 rolls.

Original change's description:
> analyzer: Report missing return for nullable return types
>
> This is implemented as a new HintCode, but it could be a new Warning, if
> we'd like to stop adding new Hints.
>
> There is also a dartfix available in this change.
>
> Fixes #46656
>
> Change-Id: I8e93e576d2bd09a8ff02d52c12bbb9ec6adff9c2
> Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/220803
> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
> Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
> Commit-Queue: Samuel Rawlins <srawlins@google.com>

TBR=scheglov@google.com,brianwilkerson@google.com,srawlins@google.com

Change-Id: I122be4cb1d07f7f95334d2d5cbac1de4bd597d15
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/224950
Reviewed-by: Alexander Thomas <athom@google.com>
Commit-Queue: Alexander Thomas <athom@google.com>
copybara-service bot pushed a commit that referenced this issue Dec 22, 2021
This is a reland of 93bcdf0

Original change's description:
> analyzer: Report missing return for nullable return types
>
> This is implemented as a new HintCode, but it could be a new Warning, if
> we'd like to stop adding new Hints.
>
> There is also a dartfix available in this change.
>
> Fixes #46656
>
> Change-Id: I8e93e576d2bd09a8ff02d52c12bbb9ec6adff9c2
> Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/220803
> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
> Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
> Commit-Queue: Samuel Rawlins <srawlins@google.com>

Change-Id: I369b2dc38db0dc278f50b55aab67da53a5a63a8d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/225400
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Commit-Queue: Samuel Rawlins <srawlins@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzer-warning Issues with the analyzer's Warning codes area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P2 A bug or feature request we're likely to work on type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

7 participants