W1022 Comparison always evaluates to True (Delphi)

From RAD Studio
Jump to: navigation, search

Go Up to Error and Warning Messages (Delphi) Index

The compiler has determined that the expression will always evaluate to true. This most often can be the result of a boundary test against a specific variable type, for example, a Integer against $80000000.

In versions of the Object Pascal compiler prior to 12.0, the hexadecimal constant $80000000 would have been a negative Integer value, but with the introduction of the int64 type, this same constant now becomes a positive int64 type. As a result, comparisons of this constant against Integer variables will no longer behave as they once did.

As this is a warning rather than an error, there is no standard method of addressing the problems: sometimes the warning can be ignored, sometimes the code must be rewritten.


program Produce;

  var
    i : Integer;
    c : Cardinal;

begin
  c := 0;
  i := 0;
  if c >= 0 then
    Writeln('true');

  if i < $80000000 then
    Writeln('true');
end.

Here the compiler determines that the two expressions will always be true. In the first case, a Cardinal, which is unsigned, will always be greater or equal to 0. In the second case, a 32-bit Integer value will always be smaller than an int64 value of $80000000.