Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2012-05

missing compiler warning in for loop - is this intentional?

2 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

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" 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

Reply by email

Florian Klämpfl
to Bernd
Bernd wrote 30 lines
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.

Reply by email