3

Each time I assign a variable without using it...

var
  X : integer;
begin
  X := 123;
end;

On compiling, a hint comes to help me saying:

[dcc32 Hint] Unit1.pas(30): H2077 Value assigned to 'X' never used

Unfortunately, it doesn't occur in case the unused variable is a string.

var
  X : string;
begin
  X := 'hello';
end;

I've reproduced the same behavior on Delphi 2007 and Delphi XE7 so I think it is an expected behavior.

Why H2077 hint is not raised for string variables?

Fabrizio
  • 7,603
  • 6
  • 44
  • 104

1 Answers1

3

It seems that managed types do not raise hints when being assigned and unused.

Other than that, when optimization is on, the assignment is not removed either, like value types are.

Why hints are not raised, is open for speculation. There is no valid reason why they should not.


See also No hint generated for assigned but unused "reference to procedure", reported in QP.

LU RD
  • 34,438
  • 5
  • 88
  • 296
  • 3
    Probably because the variable *IS* actually used, just not in the *user's code*, but in the compiler's *implicitly generated management code*. – Remy Lebeau Apr 02 '19 at 19:50
  • @RemyLebeau: I think you're right. I suspect that's have something to do with string [local variables initialization](https://stackoverflow.com/a/132770/4528159) – Fabrizio Apr 03 '19 at 06:42
  • 1
    @Fabrizio, even if that is true, there is no reason why the compiler cannot detect this condition and raise a hint and even remove the handling of the unused but assigned variable when optimization is on. – LU RD Apr 03 '19 at 07:26
  • You're right too. It's probably a bug, mine was a supposition about the causes – Fabrizio Apr 03 '19 at 07:37