A collection is searched for a value of a type its elements or keys can never have
What it finds
ids.contains(id) with a List<Long> and an int id is always false: the int is boxed to an Integer, and an Integer never equals a Long. contains, remove, indexOf, get and containsKey take any Object, so the compiler lets a value of the wrong type through, and the lookup silently finds nothing.
Reported when the collection's declared type arguments and the argument's type are both strings, boxes or primitives, and they differ after boxing.
From the rule's source, mismatched_lookup_type.rs.
import java.util.List;import java.util.Map;import java.util.Set; class Lookups { boolean find(List<Long> ids, int id, Map<String, Integer> counts, Set<String> names, long wide) { boolean a = ids.contains(id);tx0 reports: id can never equal an element of type Long boolean b = ids.contains(wide); Integer c = counts.get(42);tx0 reports: 42 can never equal an element of type String boolean d = names.contains("x"); ids.remove(0); return a || b || d; }} Turn it on, off or suppress it
The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:
[rules]
"tx0/java:mismatched-lookup-type" = "off"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:mismatched-lookup-type] reason="..."tx0 explain tx0/java:mismatched-lookup-type shows the same in the terminal. Found a false positive? Report it.