An element of a collection is assigned twice in a row with the same key, so the first value is lost

tx0/csharp:overwritten-entryrecommendedwarningreliability/logicdepth fastbug

What it finds

map[key] = a; followed by map[key] = b; replaces the first value before anything can read it: the first statement does nothing but evaluate its value. Usually one of the keys was meant to be another, after copying the line. The rule looks at statements next to each other in one block, both plain assignments to an element with the same receiver and the same key, written without calls or increments, where the second value does not read the receiver. The protected block of a try is left alone, as there the first value may be what a handler sees, and so is test code, which sets an entry twice to test the setter.

From the rule's source, overwritten_entry.rs.

Example from the rule's tests
using System.Collections.Generic; class Headers{    void Fill(Dictionary<int, string> map, int[] slots, int i)    {        map[1] = "text/html";tx0 reports: map[1] is assigned again on the next statement before it is read        map[1] = "application/json";        map[2] = "a";        map[3] = "b";        slots[i] = 1;        slots[i] = slots[i] + 1;        slots[i++] = 1;        slots[i++] = 2;        try        {            slots[0] = 1;            slots[0] = 2;        }        finally        {        }    }}
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 from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

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

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