Skip to content

SONARJAVA-5982 S106 Should not be raise on compact source files#5413

Merged
tomasz-tylenda-sonarsource merged 1 commit intomasterfrom
tt/SONARJAVA-5982_println_on_compact_cources
Feb 4, 2026
Merged

SONARJAVA-5982 S106 Should not be raise on compact source files#5413
tomasz-tylenda-sonarsource merged 1 commit intomasterfrom
tt/SONARJAVA-5982_println_on_compact_cources

Conversation

@tomasz-tylenda-sonarsource
Copy link
Contributor

No description provided.

@hashicorp-vault-sonar-prod
Copy link

hashicorp-vault-sonar-prod bot commented Jan 29, 2026

SONARJAVA-5982

@tomasz-tylenda-sonarsource tomasz-tylenda-sonarsource force-pushed the tt/SONARJAVA-5982_println_on_compact_cources branch from 8e23e5f to ce5b55b Compare February 2, 2026 16:58
@tomasz-tylenda-sonarsource tomasz-tylenda-sonarsource force-pushed the tt/SONARJAVA-5982_println_on_compact_cources branch from 3cbfb49 to 4809d73 Compare February 2, 2026 17:04
@sonarqube-next
Copy link

sonarqube-next bot commented Feb 2, 2026

@tomasz-tylenda-sonarsource tomasz-tylenda-sonarsource marked this pull request as ready for review February 2, 2026 17:18

class RegularClass {
void log(String message) {
System.out.println(message);
Copy link
Contributor

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?

Copy link
Contributor Author

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. RegularClass is nested under an unnamed implicit class, so it will not be reused and is likely related to the main method, so we should not require a logger there either. WDYT?

Copy link
Contributor

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:

The motivation for this work is, moreover, not only to help beginning programmers. We aim to help everyone who writes small programs, whether they be students, system administrators writing command-line utilities, or domain experts prototyping core algorithms that will eventually be used in the heart of an enterprise-scale software system.

So how can we be sure who wrote this specific snippet?

Copy link
Contributor Author

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?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, good idea!

Copy link
Contributor

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.

Copy link
Contributor

@asya-vorobeva asya-vorobeva left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Contributor

@rombirli rombirli left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with small comment, IMO we should raise on IO.println as it is equivalent to System.out.println

Comment on lines 56 to +61
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.");
Copy link
Contributor

Choose a reason for hiding this comment

The 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. ... ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tomasz-tylenda-sonarsource tomasz-tylenda-sonarsource merged commit b9dcc06 into master Feb 4, 2026
27 checks passed
@tomasz-tylenda-sonarsource tomasz-tylenda-sonarsource deleted the tt/SONARJAVA-5982_println_on_compact_cources branch February 4, 2026 08:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants