Shorthand Operator

ID

swift.shorthand_operator

Severity

info

Remediation Complexity

medium

Remediation Risk

low

Remediation Effort

medium

Resource

Code Smell

Language

Swift

Tags

code-smell, style

Description

Reports an assignment of the form x = x op rhs where op is one of + - * / %. Swift’s compound-assignment operators express the same intent more compactly.

x = x + 1          // FLAW — prefer x += 1
y = y * factor     // FLAW — prefer y *= factor

x = y + 1          // OK — different operand on the LHS
x = 1 + x          // OK — not the canonical form

Rationale

The compound-assignment operator names the operation as a mutation; the long form looks like a fresh computation, which encourages the reader to compare the two operands.

Remediation

Use the compound operator:

x += 1
y *= factor