1

I have an unexpected W1035 on compiling:

[dcc32 Warning] Unit1.pas(40): W1035 Return value of function 'Test' might be undefined

function CheckFn() : Boolean;
begin
  Result := True;
end;

function Test() : Boolean;
begin
  try
    if(not CheckFn()) then
      raise Exception.Create('Error Message');

    Result := True;
  finally

  end;
end;

If I remove the try-finally block, then the warning disappears.

function Test() : Boolean;
begin
  if(not CheckFn()) then
    raise Exception.Create('Error Message');

  Result := True;
end;

Why is this happening? (Bug?)

Fabrizio
  • 7,603
  • 6
  • 44
  • 104
  • Similar problem with an exception raised in `try-finally` block: `var Tmp : Integer; begin try raise Exception.Create('Invalid value!'); finally end; ShowMessage(IntToStr(Tmp)); end;`. It raises a warning on compiling _W1036 Variable 'Tmp' might not have been initialized_ – Fabrizio Oct 15 '21 at 07:44

1 Answers1

2

Let's analyse Test.

  1. If CheckFn raises an exception, you immediately go to the finally clause, and then you leave the function without returning a value.
  2. Otherwise, if CheckFn returns True, you will return True from the function.
  3. Otherwise, if CheckFn returns False, you will raise an exception and immediately go to the finally clause and then you leave the function without returning a value.

Hence, in all cases when this function does return a value, it is defined (specifically, it is True). Therefore, the compiler is wrong to emit this warning.

And, indeed, in Delphi 10.4, no warning is produced for this code.

(Or, just possibly, did you confuse finally with except? If so, the compiler is right.)

Andreas Rejbrand
  • 105,602
  • 8
  • 282
  • 384