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

Remove unused variables potential dangerous behavior #237

Closed
xczar0 opened this issue Jan 23, 2024 · 4 comments
Closed

Remove unused variables potential dangerous behavior #237

xczar0 opened this issue Jan 23, 2024 · 4 comments
Assignees
Labels
bug Something isn't working

Comments

@xczar0
Copy link

xczar0 commented Jan 23, 2024

Delete unused variables do not take into consideration potential use of dynamically assigned variables.
Code will not analyze that scenario below (simpliest one)
DATA: lv_unused_b TYPE string.
assign ('lv_unused_b') to field-symbol(<test>).
In my opinion it should detect potential dynamic usage of variable and instead of deletion of variable it should add TODO.
I mean - it adds TODO, but for completely different reason - assigned, but not used variable
image

@fabianlupa
Copy link

fabianlupa commented Jan 23, 2024

Not sure about how the detection would reliably work? I am now also wondering how it works in ADT (and ATC).

DATA: lv_unused_b TYPE string.
DATA(lv_target) = 'LV_UNUSED_' && sy-abcde+1(1).
assign (lv_target) to field-symbol(<test>).

Also includes, enhancements and probably many more.

Edit: Todo instead of deletion seems better to me, then it doesn't have to be perfect.

@xczar0
Copy link
Author

xczar0 commented Jan 24, 2024

Hi!
I think this detection mechanism should look into ASSIGN ('varname') statement or ASSIGN (directVariable), if exists and check through local coding for already existing varname somewhere else.
If it was declared in local coding - then stop it from doing fancy stuff with that particular name.
Distinction between regular ASSIGN and dynamic assign is dynamic one contains () part, where in between you have variable name.
If you assign strings you simply do `assign '' to .
I Agree - doesn't have to be perfect, but on the other hand rule of refactoring is to not break, but improve :)
And also - i know dynamic coding is more problematic thing, because of course, it's dynamic.
If this cannot be fixed, maybe it should be mentioned in some "Potential Issues" document or in the check itself?

@jmgrassau jmgrassau added the bug Something isn't working label Feb 9, 2024
@jmgrassau jmgrassau self-assigned this Feb 9, 2024
@jmgrassau
Copy link
Member

Hi xczar0,

thanks for this finding! I agree, it's better to miss a few cases (simply keeping them as they are) than to break things, and as Fabian nicely pointed out, it would be impossible to reliably detect which variable is the target of the dynamic ASSIGN.

Therefore, I think it is best to simply deactivate the affected rules ("Report unused parameters", "Delete unused variables", "Use FINAL for immutable variables") for methods that contain such a dynamic ASSIGN. Probably (and hopefully!) this syntax is anyway not used very often… Will be fixed with the next release!

Kind regards,
Jörg-Michael

@jmgrassau
Copy link
Member

Hi xczar0,

thanks again for pointing this out – version 1.13.1, which was just released, now blocks methods for "Delete unused variables" etc. if a dynamic ASSIGN is found (see details above)!

Kind regards,
Jörg-Michael

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants