Skip to content

Issue warning when indexing Map<K, V> with type other than K #58176

Closed
@averynortonsmith

Description

@averynortonsmith

The operator [] method for a map of type Map<K, V> has the type V operator [](Object key); that is, it permits keys of any type. However, accessing a map with a key type other than K often indicates the presence of a bug. It would be helpful to have a linter rule that detects these situations.

In the following example, the new rule would create a warning about the expression m[false], since m will never contain keys of type bool:

void main() {
  Map<int, String> m = {1: "a"};
  print(m[false]);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions