Skip to content

Commit 66bc890

Browse files
benzonicoclaude
andcommitted
SONARJAVA-4698 Fix false positive in S1191 when variable is named "sun"
Rule S1191 was incorrectly reporting issues when a variable (field, parameter, or local variable) was named "sun", even though its type was not from the sun.* package. The fix adds semantic type checking to verify that the expression's type actually comes from a sun.* package before reporting an issue. This prevents false positives while maintaining detection of real sun.* package usage. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 504604f commit 66bc890

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed
Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import java.util.ArrayList;
22

33
class SunPackagesUsedCheckSample {
4+
private Object sun; // variable named "sun"
5+
46
private void f() {
57
java.util.List a;
68
sun.Foo b; // Noncompliant
79
// ^^^^^^^
810
sun.Foo.toto.asd c; // secondary
911
// ^^^^^^^^^^^^^^^^<
10-
12+
1113
}
1214

1315
public Object uselessMethod() {
@@ -16,4 +18,18 @@ public Object uselessMethod() {
1618
}
1719
return null;
1820
}
21+
22+
// SONARJAVA-4698: False positive when variable is named "sun"
23+
public void fooWithFieldNamedSun() {
24+
sun.toString(); // Compliant - FP: "sun" is a field of type Object, not a sun.* package class
25+
}
26+
27+
public void barWithParameterNamedSun(Object sun) {
28+
sun.toString(); // Compliant - FP: "sun" is a parameter of type Object, not a sun.* package class
29+
}
30+
31+
public void bazWithLocalVariableNamedSun() {
32+
Object sun = new Object();
33+
sun.toString(); // Compliant - FP: "sun" is a local variable of type Object, not a sun.* package class
34+
}
1935
}

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ private void reportIssueWithSecondaries(JavaFileScannerContext context) {
6666
@Override
6767
public void visitMemberSelectExpression(MemberSelectExpressionTree tree) {
6868
String reference = ExpressionsHelper.concatenate(tree);
69-
if (!isExcluded(reference) && isSunClass(reference)) {
69+
if (!isExcluded(reference) && isSunClass(reference) && isActuallySunPackage(tree)) {
7070
reportedTrees.add(tree);
7171
}
7272
}
@@ -75,6 +75,19 @@ private static boolean isSunClass(String reference) {
7575
return reference.startsWith("sun.");
7676
}
7777

78+
private static boolean isActuallySunPackage(MemberSelectExpressionTree tree) {
79+
// Check if the expression's type actually comes from a sun.* package
80+
// This prevents false positives when a variable is named "sun"
81+
var type = tree.expression().symbolType();
82+
if (type.isUnknown()) {
83+
// If we can't determine the type, we should still check to avoid missing real issues
84+
// In this case, rely on the string-based check
85+
return true;
86+
}
87+
String fullyQualifiedName = type.fullyQualifiedName();
88+
return fullyQualifiedName.startsWith("sun.");
89+
}
90+
7891
private boolean isExcluded(String reference) {
7992
for (String str : excludePackages) {
8093
if (!str.isEmpty() && reference.startsWith(str)) {

0 commit comments

Comments
 (0)