A map entry or array element is written twice in a row, losing the first value

tx0/java:overwritten-entrydefaultwarningbug

What it finds

headers.put("Accept", json); headers.put("Accept", xml); stores a value and replaces it in the next statement, before anything could read it: the first value is lost. The same holds for values[i] = a; values[i] = b;. Usually the second key or index was meant to be a different one.

Reported are two statements in a row that put to the same map with the same key, or assign to the same array element, when key and index have no side effects and the second value does not read the map or array.

From the rule's source, overwritten_entry.rs.

Example from the rule's tests
import java.util.Map; class Headers {  void fill(Map<String, String> headers, int[] values, int i) {    headers.put("Accept", "json");    headers.put("Accept", "xml"); // want "this replaces the value just stored at `"Accept"` in `headers`"    headers.put("Content", headers.get("Accept"));    values[i] = 1;    values[i] = 2;tx0 reports: this replaces the value just stored at i in values    i++;    values[i] = values[i] + 1;  }}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

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:

tx0.toml
[rules]
"tx0/java:overwritten-entry" = "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:overwritten-entry] reason="..."

tx0 explain tx0/java:overwritten-entry shows the same in the terminal. Found a false positive? Report it.