Delphi-RAG-Lint
Delphi tooling · uses clauses

Add the Missing Uses Unit

You type CodeSite.Send('x'); and the IDE says the identifier is undeclared. It does not say which unit declares it, or which of the two uses clauses should get it. The new unit-missing-from-uses rule does both, and fixes it.

Availability

unit-missing-from-uses is new in drag-lint 1.38.0-alpha. Every output below is real output of the deployed 1.38.0-alpha build, run on a small scratch fixture.

The itch came from daily work on Micronite, the quality and production-control software we ship to manufacturers: a logging call, a missing uses entry, and a hunt for the unit name, week after week.

01 — The finding

Name the unit, name the clause

The fixture has a CodeSiteLogging unit that declares a global CodeSite, and a unit that uses it without listing it:

unit uMain;
interface
procedure Run;
implementation
procedure Run;
begin
  CodeSite.Send('x');
end;
end.
drag-lint lint uMain.pas --db mu.sqlite --rule unit-missing-from-uses
uMain.pas:7:3  [warning] unit-missing-from-uses: 'CodeSite' is declared in unit CodeSiteLogging -- add it to the implementation uses clause
1 finding(s)

The rule uses the project index, so the unit that declares the name is a lookup, not a guess.

02 — Which clause

Interface if the interface needs it, else implementation

If the name is used anywhere in the interface section — a field type, a parameter, a constant — the unit belongs in the interface uses. Otherwise it goes in the implementation uses, which keeps the dependency out of every unit that uses yours. The first example above is implementation-only. This one needs the interface:

unit uForm;
interface
type
  TFormLog = class
    FLog: TCodeSiteLogger;
  end;
implementation
end.
uForm.pas:5:11  [warning] unit-missing-from-uses: 'TCodeSiteLogger' is declared in unit CodeSiteLogging -- add it to the interface uses clause
03 — The fix

Three ways to apply it

  • RAD Studio: caret on the flagged line, Ctrl+Alt+U (menu drag-lint > Uses & Dependencies > Quick-Fix: Add Unit for Undeclared at Cursor). One undo reverts it.
  • VS Code or any LSP client: the light bulb on the diagnostic offers “Add CodeSiteLogging to the implementation uses clause”.
  • Command line: --fix previews, --fix --apply writes (a .bak is kept).
drag-lint lint uMain.pas --db mu.sqlite --rule unit-missing-from-uses --fix
  replace lines 4..4 -> "implementation\n\nuses\n  CodeSiteLogging;"
autofix: 1 fixable finding(s) -- pass --apply to write

With --apply the unit becomes:

unit uMain;
interface
procedure Run;
implementation

uses
  CodeSiteLogging;
procedure Run;
begin
  CodeSite.Send('x');
end;
end.

The section had no uses clause, so one was created. An existing clause keeps its own layout. When one file lacks several units, each clause gets a single edit.

04 — The cycle check

Not every interface edge is safe

Say uCycB already uses uCycA in its interface, and now uCycA declares a field of type TCycB. Adding uCycB to uCycA’s interface would close a loop and the compiler would stop with E2047. The rule walks the uses graph first:

uCycA.pas:5:8  [warning] unit-missing-from-uses: 'TCycB' is declared in unit uCycB -- add it to the implementation uses clause and move its interface use there (the interface uses clause would create a circular unit reference: uCycA -> uCycB -> uCycA)

The message recommends the implementation clause and names the cycle path. Its fix preview writes uses uCycB into the implementation section. (The wiki page also documents a second, interface quick-fix marked with a warning; we did not exercise it here.)

05 — Ambiguity

Several candidates are listed, not fixed

If two units declare the same name, guessing would be worse than silence:

uAmb.pas:5:8  [warning] unit-missing-from-uses: 'TWidget' is declared in units uAmb1, uAmb2 -- add one of them to the uses clause

No fix is offered; you pick the unit.

06 — Limits

Honest edges

  • The index must be current. The finding appears after the unit is reindexed (on save in the IDE). After --fix --apply the CLI itself notes that the index is stale and reports 0 findings until you reindex; after reindexing, both fixed units lint clean.
  • Precision over recall. A file that uses a unit the index does not know gets no findings, because that unit could declare the name. In our fixture, a unit with uses NoSuchUnit; and an unbound CodeSite reports 0 findings.
  • Deliberately quiet elsewhere. Per the wiki page, it also stays silent for locals, members after a dot, names that may be inherited, and inactive {$IFDEF} branches. Turn it off with "disabled": ["unit-missing-from-uses"] in drag-lint-lint.json, or review one site with // dl:ok unit-missing-from-uses -- why.
As of 2026-10-29, all command output above is real output of the deployed drag-lint 1.38.0-alpha, run on a scratch fixture indexed with drag-lint index --all. The fixture uses a stub System.pas instead of a platform library. Informational notes the CLI prints to stderr are omitted. Behaviour not run here follows the wiki page “Missing unit in uses”.