"Use FINAL" rule doesn't work when method is called in COND
Hi Jörg-Michael, please take a look at the example below. There is an inline DATA declaration that is filled using COND which is calling a method. The rule "Use FINAL for immutable variables" does not change this case to FINAL.
Thanks!
REPORT yexample.
CLASS lcl_example DEFINITION.
PUBLIC SECTION.
METHODS run.
METHODS get_result
RETURNING
VALUE(rv_result) TYPE i.
ENDCLASS.
CLASS lcl_example IMPLEMENTATION.
METHOD run.
DATA(lv_test) = COND #( WHEN get_result( ) THEN 1 ELSE 2 ).
WRITE lv_test.
ENDMETHOD.
METHOD get_result.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
NEW lcl_example( )->run( ).
Hi ConjuringCoffee,
this is (or was) deliberate, because with older ABAP versions, you could get syntax errors for some cases:
" FINAL is only possible if the method call is at the last position:
FINAL(ls_data_1) = VALUE test( name = 'Test'
count = 1
id = get_any_integer( ) ).
" previously, using FINAL triggered the syntax error 'The field "LS_DATA_2-NAME" cannot be modified.'
FINAL(ls_data_2) = VALUE test( id = 1
count = get_any_integer( )
name = 'Test' ).
" previously, using FINAL triggered the syntax error 'The field "LS_DATA_3-ID" cannot be modified.'
FINAL(ls_data_3) = VALUE test( count = get_any_integer( )
id = 1
name = 'Test' ).
" previously, using FINAL triggered a syntax error:
FINAL(lv_data_4) = COND i( WHEN lv_condition = abap_true THEN get_any_integer( ) ELSE get_any_integer( ) ).
This was why cases with method calls behind the inline declaration are currently skipped by the rule "Use FINAL for immutable variables".
Actually, checking again now, these syntax errors seem to be gone, but not sure whether that's true for all releases and update statuses out there. So, this might be a typical case of an option …
Kind regards, Jörg-Michael
Interesting 🤔