Skip to content

[NNBD] Switch statements should type-promote #45357

Open
@nex3

Description

@nex3

If a nullable variable is used in the switch() clause of a switch statement, it should be promoted to non-nullable for any clauses in which the matchers are all non-nullable. For example:

int foo(int? value) {
  switch (value) {
    case 1:
    case 2:
    case 3:
      return value;

    default:
      return 0;
  }
}

This currently produces the following analysis error:

  error • A value of type 'int?' can't be returned from the function 'foo' because it has a return type of 'int'. • test.dart:7:14 • return_of_invalid_type

Metadata

Metadata

Assignees

No one assigned

    Labels

    NNBDIssues related to NNBD Releasearea-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).customer-dart-sass

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions