Skip to content

Latest commit

Β 

History

History
51 lines (35 loc) Β· 1.67 KB

no-misrefactored-assignment.md

File metadata and controls

51 lines (35 loc) Β· 1.67 KB

no-misrefactored-assignment

πŸ“ Disallow misrefactored compound assignments where the target is duplicated in the right-hand side.

πŸ’Ό This rule is enabled in the following configs: βœ… recommended, β˜‘οΈ unopinionated.

πŸ’‘ This rule is manually fixable by editor suggestions.

A compound assignment like a += a + b repeats the assignment target as an operand of the matching operator. This is almost always a refactoring mistake: it computes a = a + (a + b) (that is, 2 * a + b), when the author most likely meant a += b.

The rule reports when the target of an arithmetic or bitwise compound assignment reappears as an operand of a right-hand side binary expression that uses the same operator.

For commutative operators (+, *, &, |, ^), the target is matched in either operand (a += a + b and a += b + a). For non-commutative operators (-, /, %, **, <<, >>, >>>), only the left operand is matched, since the right-operand form (a -= b - a) has a distinct, meaningful value.

The suggested fix changes runtime behavior, so it is offered as a suggestion rather than an automatic fix.

Examples

// ❌
a += a + b;

// βœ…
a += b;
// ❌
a *= b * a;

// βœ…
a *= b;
// βœ…
a += b + c;
// βœ… The operator does not match, so this is not flagged.
a += a - b;
// βœ… Non-commutative operator with the target as the right operand.
a -= b - a;