google / google/error-prone

Detect duplicate keys in JDK 9's Map.ofEntires() factory.

Open
#602 1 comment 3 reactions 0 assignees View on GitHub
Type-NewCheck
Dominant language
Java
Stars
7.2k
Forks
820
Avg merge
5h 9m
Merged PRs (30d)
50

Description

JDK 9 has a factory of the following form:
```java
Map map = Map.ofEntries(
entry("Foo", "Bar"),
entry("Ping", "Pong"),
entry("Kit", "Kat"));
```
`entry()` here is the static method `Map.entry()` returning an immutable `Map.Entry`.

It is a _runtime_ error to provide multiple entries with the same key to this factory.
```java
Map map = Map.ofEntries(
entry("Foo", "Bar"),
entry("Ping", "Pong"),
entry("Kit", "Kat"),
entry("Foo", "Bar"));
```
A check could catch this at compile time instead of runtime. While it's obvious in this trivial example, once you get past 10 entries it becomes hard to spot. Examples with this many entries could be TLS ciphers, escape characters, HTTP/2 known headers, etc.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.