A dead store happens when a local variable is assigned a value that is not read by any subsequent instruction or when an object property is assigned a value that is not subsequently used. Calculating or retrieving a value only to then overwrite it or throw it away, could indicate a serious error in the code. Even if it's not an error, it is at best a waste of resources. Therefore all calculated values should be used.
function pow(a: number, b: number) { if(b == 0) { return 0; } let x = a; for(let i = 1; i < b; i++) { x = a * i; //Dead store because the last return statement should return x instead of returning a } return a; }
function pow(a: number, b: number) { if(b == 0) { return 0; } let x = a; for(let i = 1; i < b; i++) { x = a * i; } return x; }
This rule ignores initializations to -1, 0, 1, null
, true
, false
, ""
, []
and
{}
.