Why is this an issue?
A dead store happens when a local variable is assigned a value 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.
Therefore all calculated values should be used.
Noncompliant code example
i = a + b; // Noncompliant; calculation result not used before value is overwritten
i = compute();
Compliant solution
i = a + b;
i += compute();
Exceptions
No issue is reported when
- the analyzed method body contains
try
blocks,
- a lambda expression captures the local variables, or
- the variable is unused (case covered by Rule S1481)
- initializations to
-1
, 0
, 1
, null
, true
, false
, ""
and string.Empty
.
Resources
- MITRE, CWE-563 - Assignment to Variable without Use ('Unused Variable')