Skip to content

unnecessary_cast false positive with ternary operator when casting a List to an Iterable #49550

Closed
@DetachHead

Description

@DetachHead
void main(final List<String> value) {
  final foo = value.isEmpty
      ? ([value[0]] as Iterable<String>) //warning: Unnecessary cast.
      : [value].map((final value) => value);
  foo.map;
}

when removing the cast:

void main(final List<String> value) {
  final foo = value.isEmpty
      ? [value[0]]
      : [value].map((final value) => value);
  foo.map; //error: The getter 'map' isn't defined for the type 'Object'
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2A bug or feature request we're likely to work onlegacy-area-analyzerUse area-devexp instead.type-bugIncorrect behavior (everything from a crash to more subtle misbehavior)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions