Delphi try..finally exit behavior change between versions 10.1 and 10.2

Viewed 178

I maintain a Delphi component that has to run in several Delphi versions. In the last few versions I have noticed a behavior change.

The following code gives a warning in Delphi 10.1, and compiles fine in Delphi 10.2:

[dcc32 Warning] asdf.pas(1179): W1035 Return value of function 'TSomeClass.SomeFunc' might be undefined

function TSomeClass.SomeFunc(objc: TObject; const xD: array of string): integer;
var
  s: string;
  i: Integer;
begin
  try
    repeat
      s := ReadLn;

      // more code here

      for i := 0 to High(xD) do
      begin
        if s = xD[i] then
        begin
          // Result := 0;
          exit;
        end;
      end;

      // more code here

    until False;
  finally
    Result := 0;
  end;
end;

The following code gives a hint in Delphi 10.2, and compiles fine in Delphi 10.1:

[dcc32 Hint] asdf.pas(1179): H2077 Value assigned to 'TSomeClass.SomeFunc' never used

function TSomeClass.SomeFunc(objc: TObject; const xD: array of string): integer;
var
  s: string;
  i: Integer;
begin
  try
    repeat
      s := ReadLn;

      // more code here

      for i := 0 to High(xD) do
      begin
        if s = xD[i] then
        begin
          Result := 0;
          exit;
        end;
      end;

      // more code here

    until False;
  finally
    Result := 0;
  end;
end;

Was this behavior changed ?

1 Answers

The behaviour of the code generated by the compiler has not changed. Your code will execute in the same way in all versions of the compiler.

However, the hints and warnings are wrong in 10.1. These are compiler bugs fixed by 10.2. The first example should not generate warning W1035. The second example should generate hint H2077.

Related