Skip to content

Hint and/or lint for changing List/Set/Map from to of #58359

Open
@rakudrama

Description

@rakudrama

Many uses of List.from can be changed to List.of.
The reasons to do so:

  • List.of is nicer to use because the extra constraint helps type inference and editor suggestions
  • List.of is more efficient
  • There is an education problem that since List.of is newer, there are lots of poor examples of List.from that could have been migrated to List.of.

Examples that could be hints (i.e. no false positives):

  • The explicit or inferred type argument E in List<E>.from(iterable) matches the static type of the iterable argument
  • The explicit type argument E in List<E>.from(iterable) is a supertype of the static type of the iterable argument

More complicated is where dynamic creeps in.
I have seen examples like:

void foo(Map<String, String> map) {
  for (final key in List.from(map.keys)) {
    print(key.isEmpty);
...

If we change List.from to List.of, the inferred type of key moves from dynamic to String. This would be helpful to the developer for completion suggestion, since key.isE• can now only complete to key.isEmpty, and not key.isEven.
We would want to suggest the replacement if the new inferred types do not lead to new or different errors or warnings or a different resolution of elements (e.g. a dynamic instance method call becoming a static extension method call).

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3A lower priority bug or feature requestarea-devexpFor issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.devexp-linterIssues with the analyzer's support for the linter packagelinter-lint-requesttype-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions