Skip to content

Latest commit

Β 

History

History
45 lines (30 loc) Β· 1.47 KB

no-useless-compound-assignment.md

File metadata and controls

45 lines (30 loc) Β· 1.47 KB

no-useless-compound-assignment

πŸ“ Disallow useless compound assignments such as x += 0.

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

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

A compound assignment by an identity value, like x += 0 or x *= 1, does not change the value and is almost always dead code left over from a bad refactor.

This rule only flags arithmetic operators where the right-hand side is the literal identity value: += 0, -= 0, *= 1, /= 1, and **= 1.

Bitwise operators such as x |= 0, x >>>= 0, and x &= -1 are not flagged, because they are deliberate idioms for coercing a value to a 32-bit integer rather than mistakes.

Because the result of these operations depends on the runtime value of the variable (for example, "a" += 0 produces "a0", "5" *= 1 produces the number 5, 5n *= 1 throws a TypeError, and x += 0 turns -0 into +0), the fix is offered as a suggestion rather than an automatic fix.

Examples

// ❌
let total = getTotal();
total += 0;

// βœ…
let total = getTotal();
// ❌
price *= 1;

// βœ…
// (remove the line)
// ❌
return count -= 0;

// βœ…
return count;