A local variable is assigned and then assigned again on the next statement, so the first value is never read
What it finds
Two assignments in a row to the same local variable, the second not reading it, make the first one useless: its value is replaced before anything sees it. Often the second assignment was meant for another variable, or the first one is left over from an edit. The rule looks at statements next to each other in one block, both plain assignments with = to the same local variable or by-value parameter. In the block of a try, the first value may be what a catch or finally sees when the second assignment throws, so those blocks are left alone.
From the rule's source, overwritten_before_read.rs.
class Totals{ int Sum(int a, int b) { int total = 0; total = a;tx0 reports: the value assigned to total is replaced on the next statement before it is read total = b; int kept = 0; kept = a; kept = kept + b; return total + kept; }} 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/csharp:overwritten-before-read" = "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/csharp:overwritten-before-read] reason="..."tx0 explain tx0/csharp:overwritten-before-read shows the same in the terminal. Found a false positive? Report it.