Linter · Rules
no-useless-assignment
Disallow assigning a value to a variable that is never read afterwards
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
Reported:
function label(user) {
let name = "guest";
name = user.name;
return name;
}
function finish(job) {
let status = job.status;
log(status);
status = "done";
}
Not reported:
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:
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:
function finish(job) {
let status = job.status;
status = "done";
}