Al: TestField does not support Enums / raises a warning

Created on 5 Mar 2020  路  13Comments  路  Source: microsoft/AL

There is not much more to add: TestField seems to cast the enum to an Integer, even though the field is defined as an Enum, and that raises a casting warning:

An implicit conversion is being performed from a value of type 'Enum "Gen. Journal Document Type"' to a value of type 'Integer'. This conversion can lead to unexpected runtime issues. This warning will become an error in a future release.AL(AL0603)

image

This was mentioned as a comment in ticket #5693 , but its main subject was unrelated, so I thought that it would be useful to raise this bug on its own.

This was tested with version 5.0.236243 of the AL extension.

bug in-progress

Most helpful comment

@atoader We have set the pipeline to fail on warnings, as @bjarkihall guessed to keep the code clean. Now we needed to switch to "unsafe" way when warnings are accepted or we need to hide them through ruleset. But there is danger that the rule will be there forever, because nobody will clean it up in all our apps later...

All 13 comments

I think you can rewrite as:
image

The issue I get now: I can assign a integer value for an Enum field.
image

@hgnamvn The irony is that, if we do that, the AL code analyzer should raise a warning, because we are actually doing an implicit casting of an enum to an Integer (in the first param of TestField, when the method gets the value).

That being said, nice to know, at least that can help us ignore some of the warnings until they fix it!

Hi @salgiza ! We're going to look into it in the next week. Thanks for reporting!

To temporarily ignore this warning, I would recommend adding a rule to codecop rulest.json file.
Something like this:

{
    "id": "AL0603",
    "action": "Hidden",
    "justification": "Implicit conversion from Enum to Integer. ... Hide warning until ..."
}

This avoids the issue of temporarily adding code you don't want to your repository, ...

26 days ago and this is still a problem?

Today I have created container for versio 16.0.11240.12076-cz and tried to use the TestLibraries and import of _Microsoft_Tests-TestLibraries.app leads to plenty of these warnings. I cannot remove them because the ruleset is not used when importing the app (or can I somehow use some rules for this?). All this makes the CI pipelines failing on the warnings just hen preparing container!

This is now for us high priority.

@kine currently working on this. Why is your CI pipeline failing on this warning?

@atoader some pipelines are configured with zero-warning tolerance on e.g. master.
NavContainerHelper's Compile-AppInNavContainer parameter Failon can be set up like this but I'd also figure this would be common in custom build pipelines too, since I'd think most people would like their master to be clean, both of errors and warnings.
Otherwise warnings can slip in unnoticed and pile up over time.
I'm not sure if this is kine's case however, just guessing! :)

I just wanted to point out for people that are having issues with this rule (AL0603) that the following is possible:

image

You can see how A breaks now,
A_BadFix shows enum value -> integer conversion (AsInteger),
A_Fix shows integer -> enum value conversion (FromInteger). I had never noticed this method before since it's a static method for the enum object itself, not an enum value.

Scratch that last question, I just saw a "bug" label. Sorry for bothering you...

Just got here to report the same issue.

Just got here just to report the same issue.

@atoader We have set the pipeline to fail on warnings, as @bjarkihall guessed to keep the code clean. Now we needed to switch to "unsafe" way when warnings are accepted or we need to hide them through ruleset. But there is danger that the rule will be there forever, because nobody will clean it up in all our apps later...

Was this page helpful?
0 / 5 - 0 ratings