Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Use FINAL" rule doesn't work when method is called in COND #352

Open
ConjuringCoffee opened this issue Sep 3, 2024 · 2 comments
Open

Comments

@ConjuringCoffee
Copy link
Contributor

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( ).
@jmgrassau jmgrassau added bug Something isn't working enhancement New feature or request new option and removed bug Something isn't working enhancement New feature or request labels Sep 19, 2024
@jmgrassau
Copy link
Member

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

@ConjuringCoffee
Copy link
Contributor Author

Interesting 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants