Returning a Value from a Setter

ID

javascript.no_setter_return

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

JavaScript

Tags

dead-code, reliability

Description

Reports return <value>; inside a setter (class setter or object-literal setter property). The runtime ignores any value returned from a setter — the assignment expression in the caller still evaluates to the value being assigned, not to the setter’s return. A bare return; (no value) is fine because it just exits the setter early.

Rationale

Returning a value from a setter usually signals a mistaken expectation:

  • obj.x = setter() evaluates to the assigned value, not the setter’s return — the developer who wrote return v.toUpperCase() is probably under the impression that the assignment now reflects the upper-cased version.

  • The dead value also confuses linters and refactoring tools that assume setters are void.

// Bad
set value(v) {
  this._v = v;
  return true;            // ignored
}

set name(n) {
  return n.toUpperCase(); // ignored - assignment still uses the original n
}

Remediation

Drop the value, or use a regular method if the operation should produce a result.

// Good
set value(v) {
  this._v = v;
}

setName(n) {
  this._name = n.toUpperCase();
  return this._name;
}

References