Fabrizio
Fabrizio

Reputation: 8043

W1035 Return value of function 'Test' might be undefined unexpectedly appears after adding a try-finally block

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?)

Upvotes: 1

Views: 398

Answers (1)

Andreas Rejbrand
Andreas Rejbrand

Reputation: 108929

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.)

Upvotes: 2

Related Questions