Product/version: SonarQube Community Build 26.9.0.129388 (sonar-java 8.41.0.47177); also 26.7.0.124771 (sonar-java 8.34.0.44906), sonar-scanner 8.1.0.6389
Language: Java
Rule: java:S1643
Issue:
seed/LogBuilder.java appends to the field log with + in a for-each loop and S1643 reports it. variant/LogBuilder.java runs the same statement once per element via values.forEach(val -> { ... }) and isn’t reported, though it builds the same strings the same number of times.
StringConcatenationInLoopCheck only reports while a loop statement is on its stack (visitForEachStatement, visitForStatement, visitWhileStatement, visitDoWhileStatement, StringConcatenationInLoopCheck.java:133-150 at 8.34.0.44906, unchanged on master at e2ff24d5b, 2026-10-08), and it lists LAMBDA_EXPRESSION as “necessarily not part of a loop” (line 79). A lambda passed to Iterable.forEach, Map.forEach or Stream.forEach runs once per element, so treating its body as a loop body would catch this. Or the description could say only loop statements count.
Reproducer:
seed/LogBuilder.java:
import java.util.List;
public class LogBuilder {
private String log = "";
void collect(List<String> values) {
for (String val : values) {
log = log + val + ", "; // Noncompliant: S1643 reported
}
}
}
variant/LogBuilder.java:
import java.util.List;
public class LogBuilder {
private String log = "";
void collect(List<String> values) {
values.forEach(val -> {
log = log + val + ", "; // the same concatenation, once per element: no S1643
});
}
}
Expected behavior:
S1643 on log = log + val + ", "; inside the lambda in variant/LogBuilder.java, as on the same statement inside the for-each loop in seed/LogBuilder.java.
How to reproduce:
# one project per file
sonar-scanner -Dsonar.projectKey=<key> -Dsonar.sources=. \
-Dsonar.java.binaries=. -Dsonar.scm.disabled=true
See also False negative java:S1643: concatenation in a loop missed when the field is written `this.field`.
Output of the commands above:
### SonarQube Community Build 26.9.0.129388 (sonar-java 8.41.0.47177), server reports 26.9.0.129388, default Sonar way profile rule: S1643
--- SEED -- expected: reported (seed/)
java:S1220 line - Move this file to a named package.
java:S1643 line 8 Use a StringBuilder instead. <== S1643
[S1643: 1]
--- VARIANT -- expected: reported; actual: not reported (variant/)
java:S1220 line - Move this file to a named package.
java:S1602 line 7 Remove useless curly braces around statement (sonar.java.sou
[S1643: 0]