-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Comments
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. |
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. |
I would personally prefer to force you to write We made exceptions for 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 |
+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 :) |
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. |
About the idea that this situation should be flagged: 👍 Here is a situation which is related: 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. |
+1, would love to see this hint or even error be implemented. |
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>
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 |
I don't personally think having to add a |
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. |
+1. |
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>
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>
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>
…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>
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>
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>
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>
Before null safety, the analyzer would report a Hint here:
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:
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.The text was updated successfully, but these errors were encountered: