Skip to content

fix(LC0095, LC0099): resolve parameter references through the binder - #641

Merged
Arthurvdv merged 4 commits into
mainfrom
fix/lc0095-bind-parameter-references
Nov 17, 2026
Merged

Arthurvdv merged 4 commits into
mainfrom
fix/lc0095-bind-parameter-references

Conversation

@Arthurvdv

@Arthurvdv Arthurvdv commented Nov 17, 2026 •

Copy link
Copy Markdown
Member

Problem

LC0095/LC0099 decided "referenced" purely syntactically: a parameter was removed from the unused set whenever any IdentifierNameSyntax in the body carried its name, including the member name of a field or method access. So procedure Foo(Name: Text) whose body only reads MyTable.Name was never reported (false negative).

Fix

The case-insensitive name scan stays as a cheap pre-filter: binding is skipped only when no parameter name appears in the body. Otherwise the body is bound once (SemanticModel.GetOperation, pre-computed by the driver for a CodeBlockAction) and a small OperationWalker removes each name-matched candidate from a set when a bound IParameterReferenceExpression.Parameter equals it, stopping as soon as the set is empty. Candidates left in the set, plus parameters the name scan never matched, are reported. When the operation is null or the bound body IsInvalid (binding errors propagate to the block, and references under invalid nodes are missing, e.g. Rec.Init() on a record of an unresolved table), the name-scan result stands, so broken code never gets extra reports. IParameterReferenceExpression, OperationWalker.VisitParameterReferenceExpression and IOperation.IsInvalid exist in the netstandard2.0 SDK at 12.0.

Fixtures

  • HasDiagnostic/ParameterNamedLikeAccessedField.al (LC0095) and HasDiagnostic/EventSubscriberParameterNamedLikeField.al (LC0099): the body reads only MyTable.Name. Both failed before the fix.
  • HasDiagnostic/ParameterUsedAsSetRangeField.al: MyTable.SetRange(Name, 'x'); the compiler binds the bare Name to the table field, not the parameter, so the parameter is unreferenced.
  • NoDiagnostic/ParameterUsedWithDifferentCasing.al, ParameterPassedAsVarArgument.al, ParameterAssignedOnly.al: a different-casing read, a var argument and an assignment target all still bind as parameter references.
  • NoDiagnostic/ParameterUsedAsRecordReceiver.al (record parameter as receiver of SetRange/Get), ParameterUsedAsFilterValue.al (MyTable.SetRange(Name, Name): the field argument binds to the field, the value to the parameter), ParameterUsedInReportProcedure.al (report procedure filtering a dataitem with its parameter).
  • NoDiagnostic/ParameterUsedUnderInvalidNode.al (error-tolerant fixture, NoDiagnosticInDocumentWithErrors): a record parameter of an unresolved table used only as MyRecord.Init(). It fails without the IsInvalid fallback. An undeclared receiver (Unknown.DoSomething(Name)) does not exercise the fallback: the bad call keeps its argument bound to the parameter.

Review note: the three plain-identifier NoDiagnostic fixtures (assignment target, var argument, different casing) are guards for the new walker, not the failing regressions; the regressions are the HasDiagnostic fixtures above. Per-body binding is the same cost class as PC0030's walker: one GetOperation per code block that passes the cheap pre-filter.

Review finding: nodes the SDK walker skips

OperationWalker does not descend into IIsExpression, IAsExpression, IConditionalOperatorExpression, the test page accesses (ITestPartAccess, ITestFilterAccess, ITestFilterFieldAccess), IPagePartAccess, the query accesses (IQueryElementAccess, IQueryDataItemAccess), ITableKeyAccess, ITableFieldGroupAccess, the report accesses (IReportLabelAccess, IReportDataItemAccess, IReportColumnAccess, IReportLayoutAccess) or IXmlPortNodeAccess; they fall to the empty DefaultVisit (verified at the 12.0.11 netstandard2.0 floor and at 30.0.44). A parameter used only inside one of them stayed an unconfirmed candidate and was reported, a false positive the name scan never produced. NoDiagnostic fixtures ParameterUsedInIsExpression, ParameterUsedInAsExpression, ParameterUsedInConditionalExpression (gated at 14.0), ParameterUsedAsTestPageReceiver and ParameterUsedAsQueryReceiver failed before the fix.

IOperation has no Children at any version. The access kinds all have visit methods at the 12.0 netstandard floor, so the walker overrides each one to visit its Instance; a new HasDiagnostic fixture ParameterNamedLikeQueryColumn (an unused Name parameter next to MyQuery.Name) proves the receiver path does not fall back to a name match. Is/as/conditional do not exist in the 12.0 netstandard SDK the netstandard2.1 build compiles against (that binary also runs on SDK 14/15, where they do exist), so only those, plus operand-free leaves, go through a DefaultVisit override that confirms a candidate by name on the node's syntax, which restores the old behaviour inside those nodes and nowhere else. Diagnostics are reported in declaration order (IParameterSymbol.Ordinal).

The other OperationWalker subclasses in the repository may have the same gap; the audit is tracked in #642.

Base App scan

A throwaway text scan (not added to the repo) looked for non-local, non-trigger, non-subscriber, non-obsolete procedure parameters (outside objects with implements) whose name occurs in the body only right after a ., never on its own. It does not catch the SetRange field-argument shape, so it is a lower bound.

  • Base Application (w1-29.0.54011.55789): 8,135 .al files, 22,417 procedures, 33,954 parameters: 0 hits (163 parameters never mentioned at all, which the rule already reports).
  • Whole MSDyn365BC.Sandbox.Code.History checkout (all apps, 17,502 files, 65,521 parameters): 2 hits, 1 real:
    • Subscription Billing Tests/Base/ContractTestLibrary.Codeunit.al, BillingTemplateWriteFilter, parameter FieldNo: the body only calls BillingTemplate.FieldNo(Filter). Hidden before, reported now.
    • Withholding Tax/.../WithholdingTaxMgmt.Codeunit.al, WithholdingAmountJournal, parameter Post: a scan artifact (Post occurs inside the quoted field name "Wthldg. Tax Bus. Post. Group"); the rule already reported this before the change.

So the change goes in the expected direction (new reports only), with very low volume on Microsoft code.

Docs

No alcops.dev change needed: the LC0095 and LC0099 pages do not describe how references are detected.

Closes #449

# Rule Verdict
1 AC0032 True, fixed in #640
2 PC0030/PC0031 analyzer Partial: only via this.<Global> / this.<DataItem>; bare names were safe. Fixed in #639
3 PC0030 CodeFix True, fixed in #639
4 LC0095/LC0099 True, fixed here
5 "PC0037" IsHandled guard True, but the rule is PC0023; casing fixed in #638, block-scope search tracked in #637
6 LC0096 False: GlobalVariable + IsSynthesized + object gate already exclude a user-declared Rec

🤖 Generated with Claude Code

Arthurvdv and others added 3 commits November 17, 2026 16:00
A parameter counted as referenced whenever any identifier in the body
carried its name, including the member name of a field or method access
such as MyTable.Name, so such parameters were never reported. The name
scan now only pre-filters; the body is bound once and a parameter is
referenced only when a bound parameter reference resolves to it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…confirmed

The walker now removes confirmed parameters from the candidate set and
stops visiting once the set is empty. New guards cover a record parameter
used as a receiver, a parameter used as a filter value next to a field with
the same name, a report procedure filtering a dataitem, and a reference
under a binding error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…kips

OperationWalker does not descend into is/as, conditional expressions or
the test page, query, report and xmlport member accesses, so a parameter
used only inside one of them stayed an unconfirmed candidate and was
reported. The walker now confirms candidates by name inside those nodes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…lker skips

Test page, page part, query, table key and field group, report and
xmlport member accesses now visit their Instance instead of falling back
to the name scan, so a parameter named like a query column is reported.
Only is/as and conditional expressions, which the netstandard SDK cannot
name, keep the name fallback. Diagnostics are reported in declaration
order.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Arthurvdv
Arthurvdv merged commit 2dbd1a1 into main Nov 17, 2026
53 checks passed
@Arthurvdv
Arthurvdv deleted the fix/lc0095-bind-parameter-references branch November 17, 2026 15:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Audit: name-based variable resolution may violate AL scoping rules (local shadows global) in several analyzers

1 participant