-
Notifications
You must be signed in to change notification settings - Fork 706
SONARJAVA-4698 Fix false positive in S1191 when variable is named "sun" #5352
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
base: master
Are you sure you want to change the base?
Conversation
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]>
| // If we can't determine the type, we should still check to avoid missing real issues | ||
| // In this case, rely on the string-based check |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the unit tests are only checking for non-compiling code, meaning we always rely on the string based check, instead of our semantic analysis. Would it be possible to test compiling code?
|
|
||
| // SONARJAVA-4698: False positive when variable is named "sun" | ||
| public void fooWithFieldNamedSun() { | ||
| sun.toString(); // Compliant - FP: "sun" is a field of type Object, not a sun.* package class |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we should mention "FP" in the comment, it sounds confusing to me: I would rather leave only // Compliant - "sun" is a field of type Object, not a sun.* package class
| } | ||
|
|
||
| public void barWithParameterNamedSun(Object sun) { | ||
| sun.toString(); // Compliant - FP: "sun" is a parameter of type Object, not a sun.* package class |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we should mention "FP" in the comment, it sounds confusing to me: I would rather leave only // Compliant - "sun" is a field of type Object, not a sun.* package class
|
|
||
| public void bazWithLocalVariableNamedSun() { | ||
| Object sun = new Object(); | ||
| sun.toString(); // Compliant - FP: "sun" is a local variable of type Object, not a sun.* package class |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we should mention "FP" in the comment, it sounds confusing to me: I would rather leave only // Compliant - "sun" is a field of type Object, not a sun.* package class
| return true; | ||
| } | ||
| String fullyQualifiedName = type.fullyQualifiedName(); | ||
| return fullyQualifiedName.startsWith("sun."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is returning always false, i.e. we never have a test case where the type is known and that its fully qualified name starts with "sun."
| // This prevents false positives when a variable is named "sun" | ||
| var type = tree.expression().symbolType(); | ||
| if (type.isUnknown()) { | ||
| // If we can't determine the type, we should still check to avoid missing real issues |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would argue that when in doubt we shouldn't report instead, to prevent raising FPs
- Remove confusing "FP:" prefix from test comments - Improve semantic analysis to use type information when available - Add compiling test cases to verify semantic analysis works correctly - When type is known, check fully qualified name; when unknown, fall back to string-based check 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
f6133df to
e7e84f4
Compare
andreaguarino
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like AutoScan task has failures on CI due to some issue differences. Can you please check that?
| if (type.isUnknown()) { | ||
| // If we can't determine the type, we should still check to avoid missing real issues | ||
| // In this case, rely on the string-based check | ||
| return true; | ||
| if (!type.isUnknown()) { | ||
| // When we have type information, use it to determine if it's actually a sun.* package | ||
| String fullyQualifiedName = type.fullyQualifiedName(); | ||
| return fullyQualifiedName.startsWith("sun."); | ||
| } | ||
| String fullyQualifiedName = type.fullyQualifiedName(); | ||
| return fullyQualifiedName.startsWith("sun."); | ||
| // When type is unknown (non-compiling code), we can't use semantic analysis | ||
| // In this case, rely on the string-based check which has already been done | ||
| // This means we may miss some FPs in non-compiling code, but we'll catch them with proper semantic analysis | ||
| return true; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like the same logic as before, with the condition reversed. In my opinion, if we don't have semantic information available, we should return false to prevent annoying FPs in our users' code, e.g. a user on SQC, with AutoScan, is going to get FPs on variables called "sun"
Address PR feedback: when semantic information is unavailable (e.g., AutoScan without bytecode or non-compiling code), return false to avoid false positives on variables named "sun" rather than risking incorrect reports. This means we might miss some true sun.* package usages in non-semantic scenarios, but users with proper builds and semantic analysis will still get accurate results. Changes: - Modified isActuallySunPackage() to return false when type is unknown - Updated test expectations: non-compiling tests now expect no issues - Removed sun.misc.Unsafe test case (doesn't compile/have bytecode) - Added detailed comments explaining the FP avoidance trade-off 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
…issue ordering - Enhanced isActuallySunPackage() to check if type is resolved, avoiding false positives in non-compiling code where variables named "sun" are misidentified as packages - Added separate visitImport() method to handle imports with proper symbol checking - Sort reported issues by line number to ensure first textual occurrence is the primary issue - Added comprehensive test coverage for all sun.* usage patterns: imports, field declarations, return types, parameters, method calls, local variables, class literals, and new expressions 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
…ection The import detection was checking only if the import symbol is known, but this can be true even in autoscan mode (without bytecode). Now we also check if the symbol's type is resolved, which requires bytecode. This ensures S1191 doesn't report any issues in autoscan mode, as expected. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
|




SONARJAVA-4698
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