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.
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.
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.
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
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:
--fixpreviews,--fix --applywrites (a.bakis 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.
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.)
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.
Honest edges
- The index must be current. The finding appears after the unit is reindexed (on save in the IDE). After
--fix --applythe 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 unboundCodeSitereports 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"]indrag-lint-lint.json, or review one site with// dl:ok unit-missing-from-uses -- why.