microsoft / AL

Home of the Dynamics 365 Business Central AL Language extension for Visual Studio Code. Used to track issues regarding the latest version of the AL compiler and developer tools available in the Visual Studio Code Marketplace or as part of the AL Developer Preview builds for Dynamics 365 Business Central.
MIT License
732 stars 243 forks source link

AL0603: comparison enum field reported as warning #5893

Closed makuhn closed 4 years ago

makuhn commented 4 years ago

Comparing an enum field with a specific value using the scope operator is displayed as a warning. Disabling the warning requires using AsInteger for both sides of expression. This doesn't make sense in this context and impairs code readability.

Actual code example from Microsoft Base Application, Codeunit 99000845 "Reservation Management", function IssueActionMessage with warning:

        SurplusEntry.TestField("Quantity (Base)");
        if SurplusEntry."Reservation Status" < SurplusEntry."Reservation Status"::Surplus then
            SurplusEntry.FieldError("Reservation Status");
        SurplusEntry.CalcFields("Action Message Adjustment");

Modified code that suppressed the warning:

        SurplusEntry.TestField("Quantity (Base)");
        if SurplusEntry."Reservation Status".AsInteger() < SurplusEntry."Reservation Status"::Surplus.AsInteger() then
            SurplusEntry.FieldError("Reservation Status");
        SurplusEntry.CalcFields("Action Message Adjustment");

Screenshots image

Version Name: AL Language Id: ms-dynamics-smb.al Description: AL development tools for Dynamics 365 Business Central Version: 5.0.271898 Publisher: Microsoft

dzzzb commented 4 years ago

Does this happen for equals or only less/greater type operators? The latter shouldn't be used anyway as it's a terrible pattern to assume that a lower integral value must always mean a lower conceptual state.

makuhn commented 4 years ago

This happens when using less/greater type operator. Using = or <> works.

dzzzb commented 4 years ago

Just IMO, less/greater should become an error with proper enums, and not be work-around-able. >:-)

atoader commented 4 years ago

Hi! In this case, the warning is correctly emitted. To perform the comparison, the compiler and the runtime will convert the enum into an integer. Considering that enums are extensible, using numerical comparison might lead to unexpected results.