A variable is assigned the result of an operation on itself, written out in full
What it finds
total = total + price names the variable twice where total += price says the same once, and makes the reader compare both sides to see that they are the same variable.
Reported are assignments to a plain variable whose value is a binary expression with that variable as the left operand and one of +, -, *, /, %, **, <<, >>, >>>, &, | or ^. The fix, which is safe since the variable is evaluated the same way, writes the compound operator.
From the rule's source, compound_assignment_candidate.rs.
export function sum(items: number[]) { let total = 0; for (const item of items) { total = total + item;tx0 reports: can be written with += } total = item2 - total; total = total * 2 + 1; return total;}declare const item2: number; Turn it on, off or suppress it
The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/ts:compound-assignment-candidate" = "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/ts:compound-assignment-candidate] reason="..."tx0 explain tx0/ts:compound-assignment-candidate shows the same in the terminal. Found a false positive? Report it.