Skip to content

"Variable assigned but never used" : False positive for MESSAGE INTO #275

Closed

Description

@HuprichT

Hi,

given the following Code:

CLASS lcl_ DEFINITION.
  PUBLIC SECTION.
    METHODS initialize.
ENDCLASS.

CLASS lcl_ IMPLEMENTATION.
  METHOD initialize.
    DATA log_msg TYPE string.

    MESSAGE w001(zbc) INTO log_msg.
  ENDMETHOD.
ENDCLASS.

After running the ABAP Cleaner, it messages that this Variable is never used, but it is clearly used here:
image

This would have led to serious problems if we had activated the setting to automatically delete unsued variables.

Activity

  1. jmgrassau commented on Mar 15, 2024

    @jmgrassau
    Member

    Hello HuprichT,

    thanks for opening this issue! First of all, no worries about deletion: This case "assigned but never used" has a setting of its own, which does not include deletion of the variable declaration (because that would of course lead to a syntax error):

    image

    And even if ABAP cleaner would ever by mistake delete such a declaration, you would get a syntax error and could undo all changes done by ABAP cleaner with Ctrl+Z (or from version history if needed).

    The comment is actually not wrong, but maybe a bit unclear: "assigned but never used" means that you write to a local variable, but never read from it, which often points to copy errors, mixed-up variables in formulas etc. Example:

    image

    So, "never used" refers to a missing read access here, although, arguably, you also "use" a variable when you write to it. But yes, in this specific case log_msg is nevertheless needed for MESSAGE to work as intended. Ideally, what you should do is add the pragma ##NEEDED to the declaration. With that, the ABAP cleaner comment will not come up – and in fact, the comment created earlier will automatically be removed by ABAP cleaner if you press Ctrl+4 again:

    image

    If you use an inline declaration for the variable here, ABAP cleaner already detects this situation and adds a hint to the comment:

    image

    So, let me see if this detection can be extended a bit to also add such a hint in your scenario (i.e. an upfront declaration of a variable that is only assigned in a MESSAGE statement but never evaluated)!

    Come to think about it, I could also try to add an option to automatically introduce a ##NEEDED pragma in this case.

    Kind regards,
    Jörg-Michael

  2. self-assigned this
    on Apr 12, 2024
  3. jmgrassau commented on Apr 12, 2024

    @jmgrassau
    Member

    Hi HuprichT,

    with the next release, the cleanup rule "Delete unused variables" will have a new option "Action for variables only assigned in MESSAGE ... INTO":

    image

    The new default, "add pragma ##NEEDED", will have the following effect:

    image

    The previous behavior would be "add TODO comment":

    image

    In both cases, ABAP cleaner can now identify these cases with upfront declarations as well (while previously, this specific "TODO … add pragma ##NEEDED" comment was only created for MESSAGE ... INTO DATA(...), i.e. for inline declarations).

    Kind regards,
    Jörg-Michael

  4. jmgrassau commented on Apr 12, 2024

    @jmgrassau
    Member

    Hi HuprichT,

    thanks again for opening this issue – the new option is now included in version 1.16.0, which was just released!

    Kind regards,
    Jörg-Michael

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions