Swift

Swift

Made by DeepSource

Unused setter value is likely a mistake SW-W1004

Bug risk
Critical

In Swift, setters should always access the value that is provided for the backing field. If a setter does not access this value, it is possible that the backing field contains a value other than what the user intended. This can result in unexpected behavior and is likely a mistake.

To fix this issue, update the setter so that it accesses the value provided for the backing field.

Bad Practice

class Person {
  private var _age: Int
  var age: Int {
    set {
      // No access to the value provided for the backing field
      _age = 18
    }
    get {
      return _age
    }
  }
}

Recommended

class Person {
  private var _age: Int
  var age: Int {
    set {
      _age = newValue
    }
    get {
      return _age
    }
  }
}