no-useless-assignment

Disallow assigning a value to a variable that is never read afterwards

warn by default

A value that's overwritten or thrown away before anything reads it does nothing. Often it's left over from an edit, or the wrong variable was assigned.

The rule follows every path through the function. An assignment is reported only if no path reads the value.

This rule is on by default.

Configure#

On by default as a warning. To turn it off:

bunfig.toml
[lint.rules]
"no-useless-assignment" = "off" # or "error"

Examples#

example.js1 problem
function label(user) {
  let name = "guest";
  name = user.name;
  return name;
}
example.js1 problem
function finish(job) {
  let status = job.status;
  log(status);
  status = "done";
}
example.jsNo problems
function label(user) {
  let name = "guest";
  if (user) {
    name = user.name;
  }
  return name;
}

A variable that an inner function reads is skipped, because that function can run at any time:

example.jsNo problems
function finish(job) {
  let status = job.status;
  job.onClose(() => log(status));
  status = "done";
}

no-unused-vars handles a variable that's never read at all:

example.jsNo problems
function finish(job) {
  let status = job.status;
  status = "done";
}