← Back to fpsonar
Seven rules, all on by default and minor. Each reports a declaration nothing appears to use. Unused declarations are not bugs, but they cost a reader attention and they hide real problems — a field nobody reads is often a field somebody forgot to write.
One caveat worth knowing before you trust these. They currently work by
matching names across the unit, not by resolving what each name refers to. So a
local variable named Count can be reported as used because an unrelated field is
also called Count, and two overloads of one routine cannot be told apart. The
effect is that they under-report — they miss real unused declarations rather than
inventing false ones. A precision upgrade is planned.
Only private members are checked for the class-member rules. A public member may be used by code fpsonar cannot see, so reporting it would be guesswork.
RemoveUnusedLocalVariable
editminor · on by default
A local variable declared and never used. Usually left behind after the code that used it was changed.
fpsonar complains about this
procedure TReport.Build;
var
Total: Integer;
Line: string; // never used
begin
Total := ComputeTotal;
WriteLn(Total);
end;
fpsonar accepts this
procedure TReport.Build;
var
Total: Integer;
begin
Total := ComputeTotal;
WriteLn(Total);
end;
RemoveUnusedConstant
editminor · on by default
A declared constant nothing references. Note the caveat above: a constant whose name also appears as a field or parameter elsewhere may escape the check.
fpsonar complains about this
const
MaxRetries = 3;
LegacyTimeout = 30; // nothing uses this
fpsonar accepts this
const
MaxRetries = 3;
RemoveUnusedGlobalVariable
editminor · on by default
A unit-level variable nothing reads or writes. These are worth removing promptly: a global that survives unused tends to attract a use later, and globals are the hardest state to reason about.
fpsonar complains about this
implementation
var
CachedResult: string; // never touched
fpsonar accepts this
implementation
RemoveUnusedField
editminor · on by default
A private field of a class or record nothing uses. Note that a field reached only
through a published property, or only by RTTI, may be reported — the name-matching
approach cannot always see those paths. Check before deleting.
fpsonar complains about this
type
TReport = class
private
FLines: TStringList;
FLegacyMode: Boolean; // never read or written
end;
fpsonar accepts this
type
TReport = class
private
FLines: TStringList;
end;
RemoveUnusedProperty
editminor · on by default
A private property nothing uses. A property is a little more suspicious than a field when unused, because writing one is a deliberate act — it suggests an intent that was abandoned.
fpsonar complains about this
type
TReport = class
private
FCount: Integer;
property Count: Integer read FCount; // nothing reads Count
end;
fpsonar accepts this
type
TReport = class
private
FCount: Integer;
public
property Count: Integer read FCount;
end;
RemoveUnusedRoutine
editminor · on by default
A private method nothing calls. Two things this rule deliberately does not report:
an operator, because dispatch is by symbol rather than name, and a method in a
type with helpers, where the call site can be elsewhere.
fpsonar complains about this
type
TReport = class
private
procedure Build;
procedure OldBuild; // nothing calls this
end;
fpsonar accepts this
type
TReport = class
private
procedure Build;
end;
RemoveUnusedType
editminor · on by default
A type declared in the implementation section that nothing names. Often the residue of a refactor that replaced one representation with another.
fpsonar complains about this
implementation
type
TRow = record
Name: string;
end;
TLegacyRow = record // nothing declares one
Name: ShortString;
end;
fpsonar accepts this
implementation
type
TRow = record
Name: string;
end;
Parameters, labels and other declarations
editEight further rules covering the declarations the ones above do not reach. All are off by default.
RemoveUnusedParameter
editminor · off by default
A parameter no statement in the routine reads. Either the routine outgrew it, or it is there to satisfy a signature — a callback shape, an interface method, an override — in which case the parameter has to stay and the finding is worth silencing rather than acting on.
fpsonar complains about this
procedure Render(aReport: TReport; aVerbose: Boolean);
begin
aReport.Draw; // aVerbose never read
end;
fpsonar accepts this
procedure Render(aReport: TReport);
begin
aReport.Draw;
end;
ParameterAssignedButNeverUsed
editminor · off by default
A value parameter that is assigned and then never read. Assigning a value parameter writes to the routine's own copy, so the write is invisible to the caller and, if nothing reads it afterwards, invisible to everyone.
Frequently this is a missing var or out.
fpsonar complains about this
procedure Normalise(aText: string);
begin
aText := Trim(aText); // the caller never sees this
end;
fpsonar accepts this
procedure Normalise(var aText: string);
begin
aText := Trim(aText);
end;
WriteOnlyVariable
editminor · off by default
A variable that is written and never read. Unlike an entirely unused variable, this one looks used — there are assignments to it — which is why it survives review.
The interesting case is when the write was meant to feed something that has since stopped reading it, so the calculation still runs and the result goes nowhere.
fpsonar complains about this
lTotal := 0;
for i := 0 to aCount - 1 do
lTotal := lTotal + Weight(i); // lTotal is never read afterwards
fpsonar accepts this
lTotal := 0;
for i := 0 to aCount - 1 do
lTotal := lTotal + Weight(i);
Result := lTotal;
UnusedExceptionVariable
editminor · off by default
An on E: ... handler variable the handler never uses. Usually a sign the handler swallows
the exception without reporting what it was — the information is right there and discarded.
fpsonar complains about this
try
Load;
except
on E: Exception do
Result := False; // E never used
end;
fpsonar accepts this
try
Load;
except
on E: Exception do
begin
LogError(E.Message);
Result := False;
end;
end;
Dropping the variable — plain except — is equally fine when you genuinely do not need it.
UnusedLabel
editminor · off by default
A declared label no goto targets. Harmless, and almost always the remains of control flow
that was restructured.
fpsonar complains about this
label
Done;
begin
Process;
end;
fpsonar accepts this
begin
Process;
end;
UnusedGenericParameter
editminor · off by default
A generic type parameter the body never names. The template takes an argument it does not use, so every specialization must supply a type that changes nothing.
fpsonar complains about this
type
generic TBox<T, U> = class // U appears nowhere below
FItem: T;
end;
fpsonar accepts this
type
generic TBox<T> = class
FItem: T;
end;
UnusedUnitInInterface
editminor · off by default
A unit imported in the interface section that only the implementation needs. Moving it
down shrinks what every consumer of your unit transitively pulls in, and can break dependency
cycles that otherwise force awkward workarounds.
Not the same as RemoveUnusedImports, which reports a unit nothing uses at all — here the
import is genuinely needed, just in the wrong section.
fpsonar complains about this
interface
uses
Classes, SysUtils; // SysUtils only used below
implementation
begin
WriteLn(FormatDateTime('yyyy', Now));
fpsonar accepts this
interface
uses
Classes;
implementation
uses
SysUtils;
PrivateMemberOnlyUsedByOneMethod
editinfo · off by default
A private member only one method of its own type references. Not a defect — a prompt. A field used by exactly one method is often a local in disguise, and a private method used by exactly one caller is often better inlined or moved.
Being info and low confidence, treat this as a design observation. Plenty of legitimate members will be reported.
fpsonar complains about this
type
TReport = class
private
FScratch: string; // only Render touches it
procedure Render;
end;
fpsonar accepts this
procedure TReport.Render;
var
lScratch: string; // a local instead
begin
Related chapters
editAn unused unit in a uses clause is RemoveUnusedImports, in
units and imports. Rules that report declarations
matching a list you configure — rather than unused ones — are the Disallowed*
family in declarations. Assignments whose value no later
statement reads are DeadStore and friends, in values through a routine.
See also
edit- fpsonar — what fpSonar is, and how to run it
← Back to fpsonar