-
Notifications
You must be signed in to change notification settings - Fork 278
AL compiler does not detect unused variables when declared in multi-variable declarations #8218
Description
Description:
The AL compiler fails to report unused variables when multiple variables are declared in a single line (comma-separated). This allows developers to unintentionally (or intentionally) bypass compiler diagnostics that would otherwise be triggered for unused variables (AA0137).
Same thing for AA0206 (initialized but not used).
This issue probably existed since the option to declare multiple variables in a single line was introduced in BC17, We've had this check in place for at least 2 years and it never triggered.
We refactored our code to make the issue visible (declare only one variable per line) and (correctly) get ~50 warnings/errors that crept in due to the check not working.
The issue is not with the "promotion" from warning to error, even if this is removed, the warning appears when the variables are declared in one line each and disappears if a variable is declared with others in the same line.
Expected Behavior:
The compiler should analyze each declared variable individually and emit warnings/errors for every unused variable, regardless of whether variables are declared:
- individually (one per line), or
- grouped in a single declaration line
Actual Behavior:
When variables are declared in a single line using comma-separated syntax, unused variable diagnostics are not triggered.
In vscode the variables show up as "0 references"
Repro Steps:
0. Create Rule to escalate AA0137 to error in ruleset.json (optional, issue is the same with warnings)
{
"name": "default ruleset",
"description": "These rules must be respected by all the AL code written within the company.",
"rules": [
{
"id": "AA0137",
"action": "Error",
"justification": "Do not declare variables that are unused."
}
]
}- Create a variable declaration using a single variable per line:
var
A: Integer;
B: Integer;-
Use only variable
Ain the code. (optional, checks are not triggered even if A and B are unused). -
Result:
- Compiler correctly reports
Bas unused.
- Now change the declaration to:
var
A, B: Integer;-
Use only variable
A. -
Result:
- Compiler does NOT report
Bas unused.
Impact:
- Violates coding guidelines where unused variables are treated as errors.
- Allows bypassing static analysis rules enforced via compiler warnings.
- Leads to reduced code quality and maintainability, especially in large legacy codebases.
- Makes automated code review and quality gates less reliable.
Workaround:
Declare each variable on its own line:
var
A: Integer;
B: Integer;Additional Notes:
This issue is especially problematic in teams enforcing strict linting rules, where warnings are escalated to errors. Developers can unintentionally bypass these checks by grouping variables.
Environment:
- AL Language version: 16.3.2065053
- Business Central version: 27.3
Suggested Fix:
Ensure the compiler performs unused-variable analysis on each identifier in a declaration list, not only on the declaration statement as a whole.
Internal work item: AB#629344