Shorthand Operator

Prefer shorthand operators (+=, -=, *=, /=) over doing the operation and assigning

  • Identifier: shorthand_operator
  • Enabled by default: Yes
  • Supports autocorrection: No
  • Kind: style
  • Analyzer rule: No
  • Minimum Swift compiler version: 5.0.0
  • Default configuration:
    KeyValue
    severity error

Non Triggering Examples

foo -= 1
foo += variable
foo *= bar.method()
self.foo = foo / 1
foo = self.foo + 1
page = ceilf(currentOffset * pageWidth)
foo = aMethod(foo / bar)
foo = aMethod(bar + foo)
public func -= (lhs: inout Foo, rhs: Int) {
    lhs = lhs - rhs
}
var helloWorld = "world!"
 helloWorld = "Hello, " + helloWorld
angle = someCheck ? angle : -angle
seconds = seconds * 60 + value

Triggering Examples

↓foo = foo * 1
↓foo = foo / aVariable
↓foo = foo - bar.method()
↓foo.aProperty = foo.aProperty - 1
↓self.aProperty = self.aProperty * 1
↓n = n + i / outputLength
↓n = n - i / outputLength