-
Notifications
You must be signed in to change notification settings - Fork 720
SONARJAVA-5982 S106 Should not be raise on compact source files #5413
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
Merged
tomasz-tylenda-sonarsource
merged 1 commit into
master
from
tt/SONARJAVA-5982_println_on_compact_cources
Feb 4, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
5 changes: 5 additions & 0 deletions
5
...t-sources/default/src/main/java/checks/SystemOutOrErrUsageCheckCompactOnlyMainSample.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| void main() { | ||
| String name = IO.readln("Enter your name: "); | ||
| IO.println("Hello " + name + "!"); | ||
| System.out.println("Goodbye " + name + "!"); | ||
| } |
21 changes: 21 additions & 0 deletions
21
...-sources/default/src/main/java/checks/SystemOutOrErrUsageCheckCompactWithClassSample.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| void main() { | ||
| String name = IO.readln("Enter your name: "); | ||
| IO.println("Hello " + name + "!"); | ||
| System.out.println("Goodbye " + name + "!"); | ||
|
|
||
| System.out.println(f(6, 7)); | ||
|
|
||
| RegularClass rc = new RegularClass(); | ||
| rc.log("This is a log message."); | ||
| } | ||
|
|
||
| int f(int x, int y) { | ||
| System.err.println("Error: x=" + x + ", y=" + y); | ||
| return x + y; | ||
| } | ||
|
|
||
| class RegularClass { | ||
| void log(String message) { | ||
| System.out.println(message); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,20 +29,36 @@ | |
| @Rule(key = "S106") | ||
| public class SystemOutOrErrUsageCheck extends IssuableSubscriptionVisitor { | ||
|
|
||
| private boolean isCompactSourceFile = false; | ||
|
|
||
| @Override | ||
| public List<Tree.Kind> nodesToVisit() { | ||
| return Collections.singletonList(Tree.Kind.MEMBER_SELECT); | ||
| return List.of( | ||
| Tree.Kind.COMPILATION_UNIT, | ||
| Tree.Kind.IMPLICIT_CLASS, | ||
| Tree.Kind.MEMBER_SELECT | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
| public void visitNode(Tree tree) { | ||
| MemberSelectExpressionTree mset = (MemberSelectExpressionTree) tree; | ||
| if (tree.is(Tree.Kind.COMPILATION_UNIT)) { | ||
| isCompactSourceFile = false; | ||
| } else if (tree.is(Tree.Kind.IMPLICIT_CLASS)) { | ||
| // System.out or System.err is allowed in compact source files. | ||
| isCompactSourceFile = true; | ||
| } else if (!isCompactSourceFile && tree instanceof MemberSelectExpressionTree mset) { | ||
| visitMemberSelectExpression(mset); | ||
| } | ||
| } | ||
|
|
||
| private void visitMemberSelectExpression(MemberSelectExpressionTree mset) { | ||
| String name = mset.identifier().name(); | ||
|
|
||
| if ("out".equals(name) && isSystem(mset.expression())) { | ||
| reportIssue(tree, "Replace this use of System.out by a logger."); | ||
| reportIssue(mset, "Replace this use of System.out by a logger."); | ||
| } else if ("err".equals(name) && isSystem(mset.expression())) { | ||
| reportIssue(tree, "Replace this use of System.err by a logger."); | ||
| reportIssue(mset, "Replace this use of System.err by a logger."); | ||
|
Comment on lines
56
to
+61
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we also raise on IO. ... as it is just a convenient way to write System.out. ... ?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. SONARJAVA-6090/ |
||
| } | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Why do we consider this as compliant?
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.
My thinking was that using a compact source file indicated that the developer, maybe a student, is writing a small script (single file) or just learning Java.
RegularClassis nested under an unnamed implicit class, so it will not be reused and is likely related to themainmethod, so we should not require a logger there either. WDYT?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.
So, if I understand correctly, we assume that if the developer is beginner, it's not required to use such sophisticated things as loggers, right? As for me, it looks like contradicting to Sonar goals - persuade developers to write better code.
And in the documentation of CSF I see the following:
So how can we be sure who wrote this specific snippet?
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 personally dislike it when I write a simple program to tests something and Sonar complaint about every single println - it's noisy and not meaningful. I thought of CSF as a good opportunity to fix something that bothers me.
Maybe we can chat about this with the entire squad during the coffee break?
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.
Yep, good idea!
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.
In my opinion, classes within a compact source file should be allowed to use System.out or IO directly, since they are only used within that same file.