Skip to content

do...while loops do not promote nullable types to non-nullable #4420

@acarstoiu

Description

@acarstoiu

Given what's written in this section, one would expect code like the following to just work:

Node? parent = ...;
if (parent == null) {
  // do something peculiar...
} else {
  do {
    parent.doThat();
    parent = parent.next;
  } while (parent != null);
}

Instead, the Dart (native) compiler warns about parent being possibly null in the loop's body. But that's plain wrong, because:

  • the first cycle is covered by the if test
  • the next cycles are possible only if parent is not null

I have other scenarios where the null safety is just annoying me without cause, but they are less obvious. I hate writing suboptimal code just to please the compiler.

Metadata

Metadata

Assignees

No one assigned

    Labels

    flow-analysisDiscussions about possible future improvements to flow analysis

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions