Unused Setter Value
ID |
swift.unused_setter_value |
Severity |
low |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
Swift |
Tags |
dead-code, reliability, suspicious-code |
Description
Reports a property or subscript setter whose body never reads the
incoming value. The implicit newValue (or the explicit name in
set(other)) is what the caller assigned — ignoring it makes the
assignment a silent no-op for that caller.
var name: String {
get { _name }
set { _name = "fixed" } // FLAW — newValue unused
}
var named: String {
get { _named }
set(other) { _named = "fixed" } // FLAW — `other` unused
}
var name: String {
get { _name }
set { _name = newValue } // OK
}
Rationale
Property assignments look like field assignments, so callers expect
obj.prop = x to store x. A setter that ignores its argument
silently breaks that contract — the most common cause is a
copy-paste from a getter that was never finished. Even when the
intent really is "ignore writes," that intent should be expressed
with a get-only property, not a setter that drops the value.