Skip to content

Avoid Variable Scope Leaking from Inner Blocks ​

WarningAnti-Patterns & Bad Practices#EC SCOPE_LEAKRemote Enabled

Overview ​

Avoid variable usage outside its declaring control block.

ATC Finding Message: Variable declared in inner block is referenced outside its scope

Rationale ​

A variable declared inside an IF or LOOP block is still visible after the block ends, so using it later creates a hidden dependency on whether the branch executed. This makes behavior hard to reason about and can leave the variable uninitialized. Declare the variable in the enclosing scope and assign it explicitly inside the branch.

Non-Compliant ​

abap
IF is_valid( ).
  DATA(lv_message) = |Order { lv_id }|.
ENDIF.
log_message( lv_message ).

Compliant ​

abap
DATA lv_message TYPE string.
IF is_valid( ).
  lv_message = |Order { lv_id }|.
ENDIF.
log_message( lv_message ).

Suppression ​

To suppress findings for this rule, append the pseudo-comment to the statement:

abap
" Standard pseudo-comment:
... "#EC SCOPE_LEAK

" SAP CI_ prefix format:
... "#EC CI_SCOPE_LEAK

Technical Details ​

Released under the MIT License.