Skip to content

NNBD: Map [] operator accepts null even when keys are non-nullable #58427

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
rrousselGit opened this issue Jun 4, 2021 · 2 comments
Closed
Labels
devexp-linter Issues with the analyzer's support for the linter package legacy-area-analyzer Use area-devexp instead. linter-lint-request

Comments

@rrousselGit
Copy link

Consider the following:

void main() {
  var map = <String, String>{};

  map[null];
}

The current behavior is, map[null] compiles (and return null)

I would expect it to emit a compilation error instead.


This appears to be voluntary, considering ma[null] = correctly does not compile.

Maybe we should have a lint?

@lrhn
Copy link
Member

lrhn commented Jun 4, 2021

The type of operator[] is V Function(Object?). Just like containsKey it accepts any key as query because it allows the object to be cast to Map<Object?, Object?> and still be useful.

We have lints for Iterable.contains, List.remove and operator==. I'm sure we could add lints for all other similarly permissive methods.

@srawlins
Copy link
Member

This is implemented in collection_methods_unrelated_type.

@devoncarew devoncarew added devexp-linter Issues with the analyzer's support for the linter package legacy-area-analyzer Use area-devexp instead. labels Nov 19, 2024
@devoncarew devoncarew transferred this issue from dart-archive/linter Nov 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
devexp-linter Issues with the analyzer's support for the linter package legacy-area-analyzer Use area-devexp instead. linter-lint-request
Projects
None yet
Development

No branches or pull requests

5 participants