Skip to content

[BUG] Replace TRANSLATE for structure #318

Closed

Description

@openPhiL

Hi.
This constellation brings a syntax error after cleanup: (Rule: "Replace TRANSLATE with string functions"

TYPES: BEGIN OF mystructure,
         string1 TYPE Char10,
         string2 TYPE char10,
       END OF mystructure.

DATA mytable TYPE TABLE OF mystructure.

LOOP AT mytable ASSIGNING FIELD-SYMBOL(<mytableline>).
  TRANSLATE <mytableline> to UPPER CASE.
ENDLOOP.

image

Activity

  1. jmgrassau commented on Jun 12, 2024

    @jmgrassau
    Member

    Hi openPhiL,

    thanks for reporting this. Hm, that's indeed a problem, because ABAP cleaner can't determine the exact type of a variable if the definition is out of sight (and that's always the case if DDIC data elements are used as the type). So we'd either have to restrict this rule to cases where the technical type is completely visible (and even those certainly aren't trivial to determine), or deactivate the rule altogether, and add a kind of "use at your own risk" warning.

    At least, since you get a syntax error right away, nothing bad can happen, since you can simply undo the cleanup in such cases. And as the rule has been around for over a year now (it was implemented in March 2023), there don't seem to be too many non-character-like cases out there…

    Kind regards,
    Jörg-Michael

  2. openPhiL commented on Jun 13, 2024

    @openPhiL
    Author

    Oh, tricky situation. Maybe a way could be to address this to someone at SAP directly. The to_upper( ) could be improved to be able to use structures as well, as long as they are charlike, that is how the old TRANSLATE` behaves anyway.

    I think defaulting to an option that only works with types that can be fully determined, plus adding an option for "even for non-determinable types" with some kind of warning/explanation would be the right approach. I have this opinion because in my understanding you have been able to build all the other rules to "theoretically" run in the background.

  3. self-assigned this
    on Jul 6, 2024
  4. jmgrassau commented on Jul 6, 2024

    @jmgrassau
    Member

    Hi openPhiL,

    thanks again for this example! I think an extra option is a good solution, so with the next release, you get:

    image

    This option will be activated by default (both for new installations and after the update), and if you deactivate it, a warning is shown:

    image

    I also enhanced the examples with some further explanation; with the option activated, none of the following will be changed:

        " unlike TRANSLATE, the string functions do not work on structured data; therefore,
        " changing the next statements might cause syntax errors. You can activate the option
        " 'Only replace TRANSLATE for known unstructured types' to restrict this cleanup rule
        " to cases in which ABAP cleaner can clearly determine the type (as above)
        DATA(ls_unknown_type) = get_charlike_structure( ).
        DATA ls_ddic_type TYPE some_ddic_type.
        DATA ls_other_type TYPE if_any_interface=>ty_s_typedef_out_of_sight.
        TRANSLATE ls_unknown_type TO UPPER CASE.
        TRANSLATE ls_ddic_type TO LOWER CASE.
        TRANSLATE ls_other_type USING 'a1b2'.

    My 40 MB real-life sample code contains 47 instances of TRANSLATE (not too many, really), of which 20 instances are still changed with the option activated (because ABAP cleaner can see that the variable type is STRING, C, N, D, T, DATN, DATS, TIMN, TIMS, SSTRING, LANG, CLIKE, CSEQUENCE, CHARx, NUMCx, or one of the character-like SY-/SYST_ fields).

    That's not perfect, but then, it would still be safe to deactivate the option, because the syntax check will immediately tell you if something goes wrong. However, definitely good to not have this as the default behavior.

    And yes, there will be additional cases in which a human reader could easily determine from the context that the type isn't structured; but I wouldn't want to take this too far for just 1 instance per MB of code – given that we have so many other promising feature requests! :-)

    Kind regards,
    Jörg-Michael

  5. jmgrassau commented on Jul 8, 2024

    @jmgrassau
    Member

    Hi openPhiL,

    thanks again for this issue! The new option is now available with version 1.18.0, which was just released!

    Kind regards,
    Jörg-Michael

  6. ConjuringCoffee commented on Jul 12, 2024

    @ConjuringCoffee
    Contributor

    Great, now we can use the TRANSLATE rule safely again! 👍

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

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions