Possible overflow assigning (PC0022)

When assigning a value from a longer text field to a shorter one, or from a larger numeric type to a smaller one, data may be truncated or overflow. This rule identifies potential overflow scenarios.

A code fix is available for this diagnostic.

Example

The following code may truncate data:

codeunit 50100 MyCodeunit
{
    procedure CopyName()
    var
        LongName: Text[100];
        ShortName: Text[20];
    begin
        LongName := 'This is a very long name that exceeds twenty characters';
        ShortName := LongName; // Possible overflow assigning [PC0022]
    end;
}

To fix this, use CopyStr to explicitly handle truncation:

codeunit 50100 MyCodeunit
{
    procedure CopyName()
    var
        LongName: Text[100];
        ShortName: Text[20];
    begin
        LongName := 'This is a very long name that exceeds twenty characters';
        ShortName := CopyStr(LongName, 1, MaxStrLen(ShortName));
    end;
}