Delphi: how to automatically remove unused vars ("Variable" x "declared but never used" hint)

Is there any tool (preferably freeware) that can parse Pascal / Delphi syntax and automatically delete unused vars?

In my case, I work with a very large Delphi code base, and in compiler hints over a thousand cases, the "variable" x is declared, but never used. "

I would spend several hours to remove them manually, and I can make mistakes, but the right tool should be able to do this safely and automatically.

I searched on the Internet but didn’t find ... does anyone know about such a tool?

Thanks...

Mark Braerford

+5
source share
4 answers

I see your point of view and completely agree that such a tool will be useful when working with outdated code. Unfortunately, I don’t know any existing tool (I have to add a free tool here, static analysis tools should, of course, be able to do this easily, but I don’t know any free static code analysis tool) that can do this.

, . . . . , , , , . , . , , .

, . , :)

, .

, . :

function ParseHint (const HintText : String; out HintInfo : THintInfo) : Boolean;
var
  I, J     : Integer;
  HintName : String;
begin
  Result := False;
  for I := 1 to Length (HintText) do
  begin
    if (HintText [I] = '(') then
    begin
      J := I + 1;
      while (HintText [J] <> ')') do Inc (J);
      HintInfo.LineNumber := StrToInt (MidStr (HintText, I+1, J-(I+1)));
      HintInfo.SourceFile := MidStr (HintText, 12, I-12);
      HintName := MidStr (HintText, J+3, 5);
      if (HintName <> 'H2164') then Exit (False);
    end;
    if (HintText [I] = '''') then
    begin
      J := I + 1;
      while (HintText [J] <> '''') do Inc (J);
      HintInfo.VarName := MidStr (HintText, I+1, J-(I+1));
      Exit (True);
    end;
  end;
end;

, , remaing . HintInfo.VarName , , '', ',' ':'. , . :

var UnusedVar : Integer;
var
  UnusedVar,
  AnotherVar : Integer;
var
  UnusedVar, AnotherVar : Integer;

, , - , , delphi , .

+4

, , Delphi. . , , .

, :

  • , .
  • .
  • .

1 2 ( ). Delphi . , .

3 . . . , , , ( ).

4 .

Aproach A (, ), ( , , , ). , . var, var !

Aproach B . , ( ). . , .

+2

, ? , , , , , , x2 x1, , ?

, , , .

:

procedure PlotPixelAtCenter(rect: Rectangle)
var
    x, y: Integer;
begin
    x := (rect.Left + rect.Right) div 2;
    x := (rect.Top + rect.Bottom) div 2; // <-- bug here, should be y :=
    PlotPixel(x, y);
end;

, . , , , , , .

+2

, , , . -, Alt-F8 ( Alt-F7 ). . . "/" , . , . , , . . - .

: . , .

+2
source

All Articles