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
declare
my_user VARCHAR2(30);
my_date VARCHAR2(30);
begin
my_user := user();
my_date := sysdate(); -- Noncompliant, the value of my_date is never read
dbms_output.put_line('User:' || my_user || ', date: ' || my_user);
end;
Compliant solution
declare
my_user VARCHAR2(30);
my_date VARCHAR2(30);
begin
my_user := user();
my_date := sysdate();
dbms_output.put_line('User:' || my_user || ', date: ' || my_date);
end;
Exceptions
This rule ignores initializations to -1, 0, 1, null
, true
, false
and ""
.
Resources
- MITRE, CWE-563 - Assignment to Variable without Use ('Unused Variable')