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

Florian Klämpfl florian at freepascal.org
Tue May 1 19:55:39 CEST 2012


Am 01.05.2012 15:20, schrieb Bernd:
> 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" 

Yes. I see a lot of cases where the loop variable itself is not used in
the loop.





More information about the fpc-pascal mailing list