Dead stores refer to assignments made to local variables that are subsequently never used or immediately overwritten. Such assignments are unnecessary and don’t contribute to the functionality or clarity of the code. They may even negatively impact performance. Removing them enhances code cleanliness and readability. Even if the unnecessary operations do not do any harm in terms of the program’s correctness, they are - at best - a waste of computing resources.
The rule ignores
-1, 0, 1, undefined, [], {},
true, false and "". _unused) are ignored. null is ignored because it is sometimes used to help garbage collection x+1
let {a, b, ...rest} = obj; // 'a' and 'b' are compliant
doSomething(rest);
let [x1, x2, x3] = arr; // 'x1' is noncompliant, as omitting syntax can be used: "let [, x2, x3] = arr;"
doSomething(x2, x3);
Remove the unnecesarry assignment, then test the code to make sure that the right-hand side of a given assignment had no side effects (e.g. a method that writes certain data to a file and returns the number of written bytes).
function foo(y) {
let x = 100; // Noncompliant: dead store
x = 150; // Noncompliant: dead store
x = 200;
return x + y;
}
function foo(y) {
let x = 200; // Compliant: no unnecessary assignment
return x + y;
}