-
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 analysis error when switch case can never match #56338
Comments
Summary: The analyzer currently doesn't flag switch cases with incompatible types, even when they can never match the switch expression. This could lead to unexpected behavior and potential bugs. |
yes this seems to be a bug, as it is not in the input format we mentioned |
can we instead update the code for switch case such that
basically treat it like how we treat a nested switch , when 2 or more conditions satisfy then that specific condition and if only one condition is mentioned then perform task as that one condition(or all the asked condition in the switch case) is satisfied |
Not an error, but there could probably be a warning from the analyzer. |
I think it's worth noting that we have only a few situations where it is statically known that a given type The general instance of this situation includes the following: class A {}
class B {} // Unrelated to `A`.
void f(A a) {
switch (a) {
case B(): // This _can_ occur.
break;
}
}
class AB extends A implements B {}
void main() {
f(AB());
} So it isn't sufficient that the two types are unrelated, we must actually know (somehow) that they are disjoint, that is, they don't have any non-empty common subtypes ( To handle disjointness of types, this could get us started:
Surely there will be more cases where we can determine that no non-empty common subtypes exist, but it could be useful to recognize a smaller set now and add more general cases later on, when we notice them. |
While debugging Dart-Code/Dart-Code#5202 I was surprised to find that there are no analysis errors when a switch case has an unrelated type to what's being tested:
Could/should there be an error in this case? Unless I'm missing something, it seems like this is probably always a bug in the users code?
The text was updated successfully, but these errors were encountered: