Skip to content

Commit 33b4cc1

Browse files
SONARJAVA-4376 Fix S2129 in the case of missing semantic (#5123)
When semantic is missing for the argument of String constructor for instance, the parser takes a guess at which constructor it is and this may cause some false positives. The fix in this PR, skips the case where we detect an unknown in the argument types.
1 parent 67d0fdd commit 33b4cc1

3 files changed

Lines changed: 34 additions & 0 deletions

File tree

java-checks-test-sources/default/src/main/java/checks/StringPrimitiveConstructorCheckSample.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package checks;
22

3+
import com.google.api.client.http.HttpResponse;
4+
import com.google.common.primitives.Bytes;
5+
import java.io.IOException;
36
import java.math.BigDecimal;
47
import java.math.BigInteger;
8+
import java.util.List;
59

610
class StringPrimitiveConstructorCheckSample {
711

@@ -57,6 +61,22 @@ void foo() {
5761
BigDecimal doubleBigDecimal = BigDecimal.valueOf(1.1);
5862
Short myShort = (short) 0;
5963
}
64+
65+
/**
66+
* When semantics is not available, the type of the argument of the constructor will be unknown.
67+
* We have to be careful to avoid false positives in that case.
68+
*/
69+
String fromHtttp(HttpResponse response) throws IOException {
70+
return new String(response.getContent().readAllBytes());
71+
}
72+
73+
/**
74+
* When semantics is not available, the type of the argument of the constructor will be unknown.
75+
* We have to be careful to avoid false positives in that case.
76+
*/
77+
public String formGuava(int i1, int i2) {
78+
return new String(Bytes.toArray(List.of(i1, i2)));
79+
}
6080
}
6181

6282
class QuickFixes {

java-checks/src/main/java/org/sonar/java/checks/StringPrimitiveConstructorCheck.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ public void visitNode(Tree tree) {
9191
if (isBigIntegerPotentiallyBiggerThanLong(newClassTree)) {
9292
return;
9393
}
94+
if (newClassTree.arguments().stream().anyMatch(arg -> arg.symbolType().isUnknown())) {
95+
// If the argument type is unknown the parser may take a wild guess at which constructor is used, which may cause false positives
96+
return;
97+
}
98+
9499
if(matchers.matches(newClassTree)) {
95100
QuickFixHelper.newIssue(context)
96101
.forRule(this)

java-checks/src/test/java/org/sonar/java/checks/StringPrimitiveConstructorCheckTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,13 @@ void test() {
3030
.withCheck(new StringPrimitiveConstructorCheck())
3131
.verifyIssues();
3232
}
33+
34+
@Test
35+
void test_without_semantics() {
36+
CheckVerifier.newVerifier()
37+
.onFile(mainCodeSourcesPath("checks/StringPrimitiveConstructorCheckSample.java"))
38+
.withCheck(new StringPrimitiveConstructorCheck())
39+
.withoutSemantic()
40+
.verifyIssues();
41+
}
3342
}

0 commit comments

Comments
 (0)