False positive squid:S1643 (Use a StringBuilder instead.)

192 views
Skip to first unread message

Paul Wagland

unread,
Nov 10, 2016, 4:22:12 PM11/10/16
to SonarQube
Hi all,

As always, hope you are all having a great day :-)

As we slowly go through our code base looking into the raised issues, I am posting through some of the false positives that we are finding that I think probably should be fixed in SonarQube. The latest that I have found:

          String[] messageBits = messageText.split("Page [0-9]");

          for (int i = 0; i < messageBits.length; i++)

          {

            if (messageBits.length > 1)

            {

              messageBits[i] = "Page " + (i + 1) + " " + messageBits[i]; // ← False positive squid:S1643 (Use a StringBuilder instead.)

            }

          }


On the above indicated line we are assigning a given string into an array of strings. There is no point in using a StringBuffer in this situation. My guess is that it is getting confused because it is doing string concatenation using "itself", however this is the only modification to the string, so there is no point in using an explicit StringBuffer, since this is already done by the compiler anyway. If this was converted to use a StringBuffer it would look like the following:

          for (int i = 0; i < messageBits.length; i++)

          {

            if (messageBits.length > 1)

            {

              messageBits[i] = new StringBuffer("Page ").append(i + 1).append(" ").append(messageBits[i]).toString();

            }

          }


I don't think anyone would say that this was an improvement :-)

Cheers,
Paul

Michael Gumowski

unread,
Nov 11, 2016, 12:31:05 PM11/11/16
to Paul Wagland, SonarQube
Hey Paul,

Thanks for the feedback and reproducer. The rule currently does not take into account the fact that the left side of the assignment could be an array access.
It will be fixed by : SONARJAVA-1949

Regards,

--
You received this message because you are subscribed to the Google Groups "SonarQube" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonarqube+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sonarqube/ed478540-9194-4466-9bbc-5a23be4e7a26%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--

Michael GUMOWSKI | SonarSource
Software Developer @ Language Team
http://sonarsource.com
Reply all
Reply to author
Forward
0 new messages