You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
"Variable assigned but never used" : False positive for MESSAGE INTO #275
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:
This would have led to serious problems if we had activated the setting to automatically delete unsued variables.
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):
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:
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:
If you use an inline declaration for the variable here, ABAP cleaner already detects this situation and adds a hint to the comment:
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
Reacted by ConjuringCoffee, Marius Raht and JoachimRees
with the next release, the cleanup rule "Delete unused variables" will have a new option "Action for variables only assigned in MESSAGE ... INTO":
The new default, "add pragma ##NEEDED", will have the following effect:
The previous behavior would be "add TODO comment":
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).
Activity
jmgrassau commentedon Mar 15, 2024
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):
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:
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_msgis nevertheless needed for MESSAGE to work as intended. Ideally, what you should do is add the pragma##NEEDEDto 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:If you use an inline declaration for the variable here, ABAP cleaner already detects this situation and adds a hint to the comment:
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
jmgrassau commentedon Apr 12, 2024
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":
The new default, "add pragma ##NEEDED", will have the following effect:
The previous behavior would be "add TODO comment":
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
enhance UnusedVariablesRule with option for MESSAGE ... INTO (SAP#275)
enhance UnusedVariablesRule with option for MESSAGE ... INTO (#275)
jmgrassau commentedon Apr 12, 2024
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