An IdentityHashMap is keyed by boxed numbers or strings, which are equal without being the same object
What it finds
An IdentityHashMap finds a key only by the very same object. Boxed numbers and strings are values: map.get(1000) boxes a new Integer that is not the one put in, so lookups fail except for the small values the box caches. The IdentityHashMap documentation warns that it breaks the Map contract on purpose.
Reported is the creation of an IdentityHashMap whose key type, written in its type arguments or in the declared type of the variable it initializes, is a box of a primitive or String.
From the rule's source, identity_map_value_key.rs.
import java.util.IdentityHashMap;import java.util.Map; class Registry { Map<Integer, String> byId = new IdentityHashMap<>();tx0 reports: keys of type Integer are compared by identity Map<String, String> byName = new IdentityHashMap<String, String>();tx0 reports: keys of type String Map<Object, String> byObject = new IdentityHashMap<>();} Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/java:identity-map-value-key" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/java:identity-map-value-key] reason="..."tx0 explain tx0/java:identity-map-value-key shows the same in the terminal. Found a false positive? Report it.