Skip to content

Conversation

@erwan-serandour
Copy link
Contributor

@erwan-serandour erwan-serandour commented Jun 16, 2025

@erwan-serandour erwan-serandour requested a review from Copilot June 16, 2025 12:07
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR updates rule S7158 to flag CharSequence.length() == 0 (and related comparisons) and suggest isEmpty(), not just for String but for all CharSequence starting in Java 15.

  • Refactors StringIsEmptyCheck to include CharSequence matchers and version gating.
  • Adds a new test case targeting CharSequence scenarios under Java 15.
  • Supplies a dedicated sample file with noncompliant and quickfix annotations for various CharSequence implementations.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
java-checks/src/test/java/org/sonar/java/checks/StringIsEmptyCheckTest.java Added charSequence() test method to verify rules under Java 15
java-checks/src/main/java/org/sonar/java/checks/StringIsEmptyCheck.java Extended check to handle CharSequence, added version guard
java-checks-test-sources/default/src/main/java/checks/StringIsEmptyCheckSample.java Introduced a charSequence method in the default sample
java-checks-test-sources/default/src/main/java/checks/CharSequenceIsEmptyCheckSample.java New sample covering various CharSequence subtypes

Comments suppressed due to low confidence (3)

java-checks/src/test/java/org/sonar/java/checks/StringIsEmptyCheckTest.java:35

  • [nitpick] Consider renaming this test method to testCharSequence for consistency with other test names like testOlderJavaVersion and test.
void charSequence() {

java-checks/src/main/java/org/sonar/java/checks/StringIsEmptyCheck.java:89

  • [nitpick] Update the Javadoc or inline comment here to explicitly mention that CharSequence.isEmpty() is only available since Java 15, while String.isEmpty() is available since Java 6.
@Override
public boolean isCompatibleWithJavaVersion(JavaVersion version) {

java-checks/src/test/java/org/sonar/java/checks/StringIsEmptyCheckTest.java:39

  • Consider adding tests for Java versions 6 through 14 to confirm that CharSequence.length() checks remain compliant (no false positives) on versions before Java 15.
.withJavaVersion(15)

}

@Test
void charSequence() {

Choose a reason for hiding this comment

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

Let's also test on Java 8, because it's still popular and CharSequence doesn't have isEmpty.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍

separate String tests samples from other charSequence tests samples and test them for each java version
import java.nio.CharBuffer;

public class CharSequenceIsEmptyCheckSample {

Choose a reason for hiding this comment

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

There are multiple occurrences of multiple blank lines in this file? Is this intentional?

public boolean testStringBuilder(StringBuilder sb1, StringBuilder sb2) {
boolean b;

b = sb1.length() == 0; // Noncompliant {{Use "isEmpty()" to check whether a "AbstractStringBuilder" is empty or not.}}

Choose a reason for hiding this comment

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

This is surprising. Why is this an AbstractStringBuilder and not a StringBuilder?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

StringBuilder does not override the method, so length is implemented inside AbstractStringBuilder. I have an idea how to solve it.

.forRule(this)
.onTree(tree)
.withMessage("Use \"isEmpty()\" to check whether a \"CharSequence\" is empty or not.")
.withMessage("Use \"isEmpty()\" to check whether a \""+lengthCall.methodSymbol().owner().name()+"\" is empty or not.")

Choose a reason for hiding this comment

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

Spaces around operators.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍


@Test
void test() {
void stringBuilder() {

Choose a reason for hiding this comment

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

Shouldn't this be just "string"? Same for stringBuilder_15 case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, indeed. good catch

@sonarqube-next
Copy link

@erwan-serandour erwan-serandour merged commit 381a07c into master Jun 18, 2025
17 checks passed
@erwan-serandour erwan-serandour deleted the erwan/SONARJAVA-5622 branch June 18, 2025 11:12
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.

2 participants