Skip to content

Expected promotion, but analyzer flags to cast and unwrap #53602

@Piinks

Description

@Piinks

Came up in flutter/flutter#133581 (comment), where the getter debugParentDataType is being added to ParentDataElement<ParentData>

There are two casts below that seem like they should not be necessary, but are flagged as necessary by the analyzer for:

  • (1) two complaints from the analyzer
    • unchecked_use_of_nullable_value (but we already know it is not null)
    • undefined_getter (but we already know it is ParentDataElement<ParentData> not Element)
  • (2) one complaint
    • undefined_getter (but we already, now definitely, know it is ParentDataElement<ParentData> not Element)
    Element? ancestor = _parent;
    while (ancestor != null && ancestor is! RenderObjectElement) {
      if (ancestor is ParentDataElement<ParentData>) {
        assert(() {
          if (!debugAncestorTypes.add(ancestor.runtimeType)) {
            debugAncestorCulprits.add(ancestor.runtimeType);
          }
          
          // Both of the following casts are required by the analyzer
          // (1)
          if (!debugParentDataTypes.add((ancestor! as ParentDataElement<ParentData>).debugParentDataType)) {
            // (2)
            if (!debugParentDataCulprits.contains((ancestor as ParentDataElement<ParentData>).debugParentDataType)) {
              // Add the first one we had put in the Set of Types.
              debugParentDataCulprits.add(ancestor.debugParentDataType);
            }
            debugParentDataCulprits.add(ancestor.debugParentDataType);
          }
          return true;
        }());
        result.add(ancestor);
      }
      ancestor = ancestor._parent;
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-languageDart language related items (some items might be better tracked at github.com/dart-lang/language).type-questionA question about expected behavior or functionality

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions