Al: AA0205 is fired too often

Created on 23 Mar 2020  Â·  7Comments  Â·  Source: microsoft/AL

Describe the bug
"AA0205 Use of unassigned variable" is fired too often

To Reproduce
Use code just like the BC standard does (SalesLine.Table):

    local procedure GetSalesSetup()
    begin
        if not SalesSetupRead then
            SalesSetup.Get();
        SalesSetupRead := true;
    end;

SalesSetupRead is marked as "Use of unassigned variable".

Expected behavior
SalesSetupRead should not be marked as a problem, although it's true that it is not assigned on first usage. As long as it is assigned at all, this rule should not fire.

Versions:

  • AL Language: 5.0.236243
  • Business Central: not relevant
CodeCop bug in-progress static-code-analysis

Most helpful comment

In relation to this issue.
I think that AA0205 should also not be triggered on page with SaveValues on true. We use global (text) variables to store data and custom filters, so users do not have to enter them again. Initializing counteracts on the save value setting at least to my opinion.

All 7 comments

Agreed, came here to report that AA0205 is not clever enough for global variables on pages. I'm not sure a rule can be written since procedures on pages can be called before the page is opened.

It would make more sense if we had variable initialization on declaration, like in C#. At least for primitive types.

Something like:
var SalesSetupRead : Boolean := false;

I should check if there is already an idea for that.

Also this warning on using Rec.Copy:
TmpHeader.Copy(Header);
TmpHeader.Insert(); - here I have assigned variable warning

Let's have a look.

For boolean it seems a bit hard to always require it to be initialised to false in this case.

Yes Copy should be initialization.

In relation to this issue.
I think that AA0205 should also not be triggered on page with SaveValues on true. We use global (text) variables to store data and custom filters, so users do not have to enter them again. Initializing counteracts on the save value setting at least to my opinion.

Agreed, came here to report that AA0205 is not clever enough for global variables on pages.

It's an issue for all globals, actually.

I just ran into the following code in a codeunit (very simplified, please don't question its sense ;-)):

procedure SomeFunction() ReturnInt : Integer;
begin
  SomeInitializationFunction(); // fills global variables, also MyListOfInteger which is used as a constant
  ReturnInt := MyListOfInteger.Get(5); // AA0205
end;

The interesting thing is: The warning appears only on the global List of [Integer] variable, but not on the Dictionary of [Char, Integer] global variables that are filled and used the very same way on the very same places.

fixed in latest master

Was this page helpful?
0 / 5 - 0 ratings