[fpc-pascal] missing compiler warning in for loop - is this intentional?

Bernd prof7bit at googlemail.com
Tue May 1 15:20:34 CEST 2012


I just had written this little faulty piece of code:

procedure TBuddyList.RemoveBuddy(ABuddy: TABuddy);
var
  I,J,Last : Integer;
begin
  EnterCriticalsection(FCritical);
  Last := Length(FList) - 1;
  for I := 0 to Last do begin
    if FList[I] = ABuddy then begin
      for J := I to Last-1 do begin
        FList[I] := FList[I+1];
      end;
      SetLength(FList, Last);
      break;
    end;
  end;
  LeaveCriticalsection(FCritical);
  Save;
end;

note the wrong variable I in the second loop, it should be J. The
variable J was assigned (in the for loop) but never actually used. I
only noticed it by accident (and I could not even test the code yet
because its still incomplete) but a warning "assigned but never used"
would have prevented this.

Is it intentional because the For loop implicitly checks the loop
variable during looping already which is also regarded as "variable is
used" or should I file a bug? Or would this introduce too many
complications and ugliness into the compiler that simply are not worth
the effort?

var
  A,B : Integer;
begin
  B := 0; // will warn about unused B
  for A := 0 to 1000 do begin
    // no warning about A
  end;
end;

Bernd



More information about the fpc-pascal mailing list