Skip to content

Enable CFA / exhaustiveness-checking for exhaustive switch statements #3891

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

Closed
mattrberry opened this issue Jun 9, 2024 · 4 comments
Closed
Labels
request Requests to resolve a particular developer problem

Comments

@mattrberry
Copy link

The compiler complains about switchStatement, reporting that the switch statement doesn't exhaustively cover the cases.

String switchStatement() {
  switch ([]) {
    case [_, ...]:
      return 'at least 1';
    case []:
      return 'none';
  }
}

String switchExpression() => switch ([]) {
      [_, ...] => 'at least 1',
      [] => 'none',
    };
main.dart:1:8: Error: A non-null value must be returned since the return type 'String' doesn't allow null.
String switchStatement() {
       ^

This is obviously a fairly contrived case, but I've seen similar patterns come up a few times now. Every problem I've had is solvable, but not always in the nicest of ways.

It seems that the type-checker only does the full exhaustiveness checking for switch expressions or for switch statements with very obvious cases, e.g. if [...] was a case in the above example. I find this to be confusing. Some more discussion here.

What are the thoughts from the language team? Is this possible to support?

@mattrberry mattrberry added the request Requests to resolve a particular developer problem label Jun 9, 2024
@srawlins
Copy link
Member

Good question. Looks like a duplicate of #2977

@mattrberry
Copy link
Author

I haven't had a chance to read all of the comments there, but that issue definitely seems to be a match. Sorry for the duplicate, I swear I tried to search the issues first 😅

@shayanzare-dev
Copy link

Good

@mattrberry mattrberry closed this as not planned Won't fix, can't repro, duplicate, stale Jun 16, 2024
@srawlins
Copy link
Member

Yeah it took me a bit to find the issue too 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
request Requests to resolve a particular developer problem
Projects
None yet
Development

No branches or pull requests

3 participants