SonarQube
  1. SonarQube
  2. SONAR-1369

CPD Plugin "Ignore Literals" causes "false" detections with big array of Strings

    Details

    • Type: Improvement Improvement
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.12
    • Fix Version/s: 2.11
    • Component/s: Duplications
    • Labels:
      None
    • Environment:
      Sonar runs on Red Hat Linux, connects to Oracle database, fetches code from CVS repository in a GForge installation.
    • Number of attachments :
      0

      Description

      We set up the "Settings -> CPD Plugin -> Ignore Literals" to be "true".

      Sonar correctly detected this code to be copy/paste (code paraphrased):
      List peopleList = new ArrayList();
      peopleList.add("a name");
      peopleList.add("some name");
      peopleList.add("another name");
      peopleList.add("some other name");

      was a copy of

      List houseList = new ArrayList();
      houseList.add("a house");
      houseList.add("some house");
      houseList.add("another house");
      houseList.add("some other house");

      For reasons beyond the scope of this bug, we need the strings to be in the code. So we changed the code into:
      String [] people = new String[]

      {"a name", "some name", "another name", "some other name"}

      ;
      and
      String [] houses = new String[]

      {"a house", "some house", "another house", "some other house"}

      ;

      The string arrays are also detected as duplicates. Although I technically understand why this is happening, I think this prevents developers from creating multiple large arrays with strings in any part of the code.

      Is this the desired effect and if so, how do the people who wrote the CPD plugin suggest this is solved, given that the strings can not be externalized (the need to be part of the code, per requirement of the customer).

        Issue Links

          Activity

          Hide
          Freddy Mallet added a comment -

          You're right Rolf.

          When the "Ignore Literals" is set to false, the statement "peopleList.add("a name").add("another name")" is tokenized to get the following tokens : 'people', 'add', 'a name', 'add', 'another name'.

          When the "Ignore Literals" is set to true, with the same statement, we get the following list of tokens : 'people', 'add', 'literal', 'add', 'literal'.

          In fact 'literal' token should not be included in the token list and we should get 'people', 'add', 'add'.

          if you agree, I'm going to change the type of this ticket from Bug to Improvement.

          Show
          Freddy Mallet added a comment - You're right Rolf. When the "Ignore Literals" is set to false, the statement "peopleList.add("a name").add("another name")" is tokenized to get the following tokens : 'people', 'add', 'a name', 'add', 'another name'. When the "Ignore Literals" is set to true, with the same statement, we get the following list of tokens : 'people', 'add', 'literal', 'add', 'literal'. In fact 'literal' token should not be included in the token list and we should get 'people', 'add', 'add'. if you agree, I'm going to change the type of this ticket from Bug to Improvement.
          Hide
          Rolf added a comment -

          You're right, moving from bug to improvement seems like a good idea. Thanks!

          Show
          Rolf added a comment - You're right, moving from bug to improvement seems like a good idea. Thanks!
          Hide
          Freddy Mallet added a comment -

          Hi Rolf, with our new clones detection mechanism (see SONAR-1091), initialization of big array of Strings or chaining calls of the same method a lot of times won't generate any duplications.

          Show
          Freddy Mallet added a comment - Hi Rolf, with our new clones detection mechanism (see SONAR-1091 ), initialization of big array of Strings or chaining calls of the same method a lot of times won't generate any duplications.

            People

            • Assignee:
              Freddy Mallet
              Reporter:
              Rolf
            • Votes:
              1 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: