Skip to content

SONARJAVA-5599 Modify S3024: the rule applies only inside loops #5141

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

Merged
merged 1 commit into from
Jun 23, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions rules/S3024/java/rule.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@ Using ``++String++`` concatenation within ``++StringBuilder.append++`` defeats t
If you concatenate only a few strings, use direct ``++String++`` concatenation.
Otherwise, replace ``++String++`` concatenation with calls to ``++append++``.

This rule applies to ++String++ concatenations performed repeatedly, inside loops. In such scenarios, the performance penalty associated with inefficient ++StringBuilder.append++ usage can multiply significantly.


=== Noncompliant code example

[source,java,diff-id=1,diff-type=noncompliant]
----
StringBuilder sb = new StringBuilder();
String s = sb.append("foo is: " + getFoo()).toString(); // Noncompliant
----

[source,java,diff-id=2,diff-type=noncompliant]
----
StringBuilder sb = new StringBuilder();
for (String name : names) {
sb.append("Hello : " + name); // Noncompliant
}
Expand All @@ -27,11 +23,6 @@ for (String name : names) {

[source,java,diff-id=1,diff-type=compliant]
----
String s = "foo is: " + getFoo();
----

[source,java,diff-id=2,diff-type=compliant]
----
StringBuilder sb = new StringBuilder();
for (String name : names) {
sb.append("Hello : ").append(name);
Expand Down
Loading