-
Notifications
You must be signed in to change notification settings - Fork 1.7k
missing_return should know about enums and switches #35710
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
Comments
From @matanlurey on August 20, 2018 23:13 I believe (Also the analyzer isn't allowed to use |
From @Hixie on August 21, 2018 0:11 Feel free to move the bug to the other repo, I don't really understand the difference between lints and errors in the analyzer. This doesn't affect program flow. The flow of the program and the return value with and without the assert are identical, regardless of the value, in every mode (though obviously debug and release would behave differently from each other). This is just about whether the missing_return lint/error/whatever should be flagged in a case where the code is redundant anyway. |
From @bwilkerson on August 21, 2018 0:15 Yes, And, yes, analyzer does not currently trust asserts.
Only if you trust that the assert means that the |
From @Hixie on August 21, 2018 0:26 What I mean is that In general I agree that we want to catch that (hence This doesn't require trusting the assert; the behaviour is the same in release mode whether you have the explicit return or not. It's just about avoiding an extra (redundant) line of code. |
From @bwilkerson on August 21, 2018 4:43 Personally, I don't see it as being redundant, I see it as being explicit. Just because there is an implicit default behavior, that doesn't mean that I intended the implicit behavior. |
From @Hixie on August 21, 2018 5:24 "Redundant" isn't a value judgement. It's literally redundant in the sense that it's the same as the implicit behaviour. It can be explicit and redundant. I think most of the time this redundant explicit statement is the right thing to do. It's just in this specific case with the switch that covers every possible value, it's actually misleading because assuming there's no bugs, it's dead code (and, once this bug is fixed, could even be called out as such, see #57781). |
From @srawlins on December 29, 2018 19:54 If explicit return values of null are redundant because it duplicates the implicit behavior, what is the request around enums and switches? It seems like #57782 would be a better solution. E.g. String f() {
if (1+1==2) {
return "hello";
}
return null
} has just as much redundant behavior as the enum/switch example. |
From @bwilkerson on December 29, 2018 21:45 The purpose of this hint is to prevent code from implicitly returning
I will also point out that it is possible that this will be less of a problem if we get non-nullable types:
Which leads to another option:
|
From @Hixie on December 29, 2018 23:21 3 would be awesome. We use lint vs hint wouldn't resolve this issue. I don't understand the difference between errors and hints: both can be turned off, if desired (by post filtering if nothing else), both show up in the analyzer output. shrug |
In Dart 2, the "implicit" return of |
Yes, this bug is about fixing that. |
@bwilkerson wrote:
Right, and it matches up with the plans for how to introduce such types. Here is the original example again: enum Foo { a, b, c }
String hello(Foo value) {
assert(value != null);
switch (value) {
case Foo.a: return 'A';
case Foo.b: return 'B';
case Foo.c: return 'C';
}
// This should not be flagged by missing_return
} NNBD (non-null by default) is a language feature that will be added to Dart. At first, we will most likely get a level of support which includes static checks, and soon after that also a level that allows manual checks (like the If the library that contains For call sites in legacy code (which does not contain an NNBD opt-in marker) we cannot expect to be able to enforce soundness in general, but with a compiler-generated null check we would actually know for sure that In any case (with or without soundness), the static analysis would consider In summary, we shouldn't need to worry so much about null here, that's coming. The other part is about exhaustiveness and reachability (specifically, it's about the property 'statically known to not complete normally'), and that's being addressed here: dart-lang/language#139. |
Bug: #35710 Change-Id: I6f17db1a9b2c01ec4b97538e224146fd3cec9002 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/149606 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
From @Hixie on August 20, 2018 23:11
If you assert that a variable of enum type is non-null before a switch, and then you have a switch where every enum value is handled and terminates (returns, throws, or exits), then it should not flag that function as not ending with a return. (The usual pattern today is to
return null
after the switch to silence the analyzer, but that's what would happen anyway if the statement wasn't there, in the case of the variable being null in a release build.)For example:
cc @a14n
Copied from original issue: dart-lang/linter#1139
The text was updated successfully, but these errors were encountered: