Skip to content

Commit f002a03

Browse files
SONARJAVA-5759 Fix NPE on S3457 on enums declaration type (#5293)
1 parent bfce739 commit f002a03

File tree

5 files changed

+38
-4
lines changed

5 files changed

+38
-4
lines changed

its/autoscan/src/test/resources/autoscan/autoscan-diff-by-rules.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1598,7 +1598,7 @@
15981598
{
15991599
"ruleKey": "S3457",
16001600
"hasTruePositives": true,
1601-
"falseNegatives": 150,
1601+
"falseNegatives": 152,
16021602
"falsePositives": 0
16031603
},
16041604
{
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"ruleKey": "S3457",
33
"hasTruePositives": true,
4-
"falseNegatives": 150,
4+
"falseNegatives": 152,
55
"falsePositives": 0
6-
}
6+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package checks;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
6+
// Introduced to handle NPE described in SONARJAVA-5759
7+
// https://sonarsource.atlassian.net/browse/SONARJAVA-5759
8+
enum PrintfMisuseCheckEnumsSample {
9+
A,
10+
B;
11+
12+
private static final Logger LOG = LoggerFactory.getLogger(PrintfMisuseCheckEnumsSample.class);
13+
14+
public static PrintfMisuseCheckEnumsSample of(String name) {
15+
if (name.equals(B.name())) {
16+
LOG.error("Connector type name {}"); // Noncompliant
17+
return B;
18+
}
19+
if (name.equals(A.name())) {
20+
LOG.error("No action needed, just FYI: connector type name {}, or {}", A); // Noncompliant
21+
return A;
22+
}
23+
return valueOf(name);
24+
}
25+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ private static boolean hasUnknownExceptionInUnionType(ExpressionTree lastArgumen
302302
}
303303
Symbol symbol = ((IdentifierTree) lastArgument).symbol();
304304
VariableTree declaration = symbol.isVariableSymbol() ? ((Symbol.VariableSymbol) symbol).declaration() : null;
305-
if (declaration == null) {
305+
if (declaration == null || declaration.type() == null) {
306306
return false;
307307
}
308308
TypeTree declarationType = declaration.type();

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,13 @@ void test_unknown_union_types() {
4747
.withCheck(new PrintfMisuseCheck())
4848
.verifyIssues();
4949
}
50+
51+
@Test
52+
void test_enums() {
53+
// see SONARJAVA-5759
54+
CheckVerifier.newVerifier()
55+
.onFile(mainCodeSourcesPath("checks/PrintfMisuseCheckEnumsSample.java"))
56+
.withCheck(new PrintfMisuseCheck())
57+
.verifyIssues();
58+
}
5059
}

0 commit comments

Comments
 (0)