H2164 Variable '%s' is declared but never used in '%s' (Delphi)

From RAD Studio

Go Up to Error and Warning Messages (Delphi)


This hint is generated when the compiler has determined that a variable is not used. Note that you may have code that uses the variable, but from the compiler’s perspective, the variable is not used. For example, one is allowed to ask an instance variable to invoke a static member, but internally the compiler rewrites the call using the underlying type name. Therefore, the instance is not used.

type
  TRec = record
    class procedure M; static;
  end;

procedure P;
var
  R: TRec;
begin
  R.M;
end;

Notice that the call to R.M is written as TRec.M by the compiler. This occurs during the parse phase, but later, when the compiler runs a data flow analysis, it finds that variable R was never used.

This hint can also be generated if the variable is used in dead code. For example, if the variable is used in code that the compiler has determined to be unreachable, the compiler will eliminate that code and may generate this hint. See the example below:

procedure Test;
var
  unused: Integer;
begin
  if False then
    Writeln(unused);
end;

For better clarity, it is recommended to refer to static members using the underlying type name; doing so eliminates the potential H2164 hint from using an instance to only access static members.

Another solution is to remove any unused variables from your procedures. However, unused variables can also indicate an error in the implementation of your algorithm.