Skip to content

Infer non-nullability from local boolean variables #1274

Closed
@yjbanov

Description

@yjbanov

It is frequently more readable to give a boolean expression a name for self-documentation. For example:

final bool isSingleElement = fields == null;
if (!isSingleElement) {
  fields.forEach(...);
}

However, this does not promote fields to non-null inside the if block. A workaround is to remove the variable:

if (fields != null) {
  fields.forEach(...);
}

However, this makes the code less readable, because non-nullness of fields does not immediately imply that we're dealing with a "single line" mode of something.

Can the nullability inference be enhanced to infer using local booleans?

/cc @nturgut @leafpetersen

Metadata

Metadata

Assignees

Labels

nnbdNNBD related issues

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions