Skip to content

Switch expressions don't exaustevely check for local functions that return exceptions. #3313

Open
@feinstein

Description

@feinstein

I tried to make the following code with switch expression.

  static FirebaseOptions forPlatform({FirebaseOptions? android, FirebaseOptions? ios}) {
    void throwUnsupportedPlatform(TargetPlatform platform) => throw UnsupportedError('DefaultFirebaseOptions are not supported for $platform.');

    return switch (defaultTargetPlatform) {
      TargetPlatform.android => android ?? throwUnsupportedPlatform(defaultTargetPlatform),
      TargetPlatform.iOS => ios ?? throwUnsupportedPlatform(defaultTargetPlatform),
      _ => throwUnsupportedPlatform(defaultTargetPlatform),
    };
  }

The analyzer complains that A value of type 'void' can't be returned from the method 'forPlatform' because it has a return type of 'FirebaseOptions'.. I imagined the analyzer would be able to know that throwUnsupportedPlatform always throws, so this is valid code.

This code works:

    return switch (defaultTargetPlatform) {
      TargetPlatform.android => android ?? (throw UnsupportedError('DefaultFirebaseOptions are not supported for $defaultTargetPlatform.')),
      TargetPlatform.iOS => ios ?? (throw UnsupportedError('DefaultFirebaseOptions are not supported for $defaultTargetPlatform.')),
      _ => throw UnsupportedError('DefaultFirebaseOptions are not supported for $defaultTargetPlatform.'),
    };

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugThere is a mistake in the language specification or in an active document

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions