HomeDelphiDelphi Error – H2077 Value assigned to ‘%s’ never used

Delphi Error – H2077 Value assigned to ‘%s’ never used

Delphi Compiler Error

H2077 Value assigned to ‘%s’ never used

Reason for the Error & Solution

The compiler gives this hint message if the value assigned to a variable is not used. If optimization is enabled, the assignment is eliminated.

This can happen because either the variable is not used anymore, or because it is reassigned before it is used.

program Produce;
(*$HINTS ON*)

procedure Simple;
var
  I: Integer;
begin
  I := 42;                (*<-- Hint message here*)
end;

procedure Propagate;
var
  I: Integer;
  K: Integer;
begin
  I := 0;                 (*<-- Hint message here*)
  Inc(I);                 (*<-- Hint message here*)
  K := 42;
  while K > 0 do begin
    if Odd(K) then
      Inc(I);             (*<-- Hint message here*)
    Dec(K);
  end;
end;

procedure TryFinally;
var
  I: Integer;
begin
  I := 0;                 (*<-- Hint message here*)
  try
    I := 42;
  finally
    Writeln('Reached finally');
  end;
  Writeln(I);             (*Will always write 42 - if an exception happened,
          we wouldn't get here*)
end;

begin
end.

In procedure Propagate, the compiler is smart enough to realize that as variable I is not used after the while loop, it does not need to be incremented inside the while, and therefore the increment and the assignment before the while loop are also superfluous.

In procedure TryFinally, the assignment to I before the try-finally construct is not necessary. If an exception happens, we don’t execute the Writeln statement at the end, so the value of I does not matter. If no exception happens, the value of I seen by the Writeln statement is always 42. So the first assignment will not change the behavior of the procedure, and can therefore be eliminated.

This hint message does not indicate your program is wrong – it just means the compiler has determined there is an assignment that is not necessary.

You can usually just delete this assignment – it will be dropped in the compiled code anyway if you compile with optimizations on.

Sometimes, however, the real problem is that you assigned to the wrong variable, for example, you meant to assign J but instead assigned I. So it is worthwhile to check the assignment in question carefully.

Share:

Leave a Reply

You May Also Like

Delphi Compiler Error X2421 Imported identifier ‘%s’ conflicts with ‘%s’ in ‘%s’ Reason for the Error & Solution This occurs...
Delphi Compiler Error X2367 Case of property accessor method %s.%s should be %s.%s Reason for the Error & Solution No...
Delphi Compiler Error X2269 Overriding virtual method ‘%s.%s’ has lower visibility (%s) than base class ‘%s’ (%s) Reason for the...