A dead store happens when a local variable is assigned a value, including null
, that is not read by any subsequent instruction.
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.
Even assigning null
to a variable is a dead store if the variable is not subsequently used. Assigning null as a hint to the garbage
collector used to be common practice, but is no longer needed and such code should be eliminated.
public void pow(int a, int b) { if(b == 0) { return 0; } int x = a; for(int i= 1, i < b, i++) { x = x * a; //Dead store because the last return statement should return x instead of returning a } return a; }
public void pow(int a, int b) { if(b == 0) { return 0; } int x = a; for(int i= 1, i < b, i++) { x = x * a; } return x; }
This rule ignores initializations to -1, 0, 1, null
, empty string (""
), true
, and false
.