Skip to content

SONARJAVA-7188 SONARJAVA-4606 Improvements for the rule S7158 - #6359

Merged
asya-vorobeva merged 5 commits into
masterfrom
asya/improve-s7158
Oct 16, 2026
Merged

asya-vorobeva merged 5 commits into
masterfrom
asya/improve-s7158

Conversation

@asya-vorobeva

@asya-vorobeva asya-vorobeva commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor
  • Detect zero literals written in any base
  • Suggest "isBlank()" for "strip().length()" and "trim().length()" comparisons

asya-vorobeva and others added 2 commits October 14, 2026 15:31
LiteralUtils.isZero compared the literal's decimal token text, so
length() == 0 was flagged but the equivalent length() == 0x0, 00 or
0b0 was not. Compare the parsed literal value instead.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
…risons

Comparing "s.trim().length()" to zero is a blankness check, so report it
with an "isBlank()" message and quick fix instead of "isEmpty()". Gated
on Java 11, where "String.isBlank()" was introduced.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@hashicorp-vault-sonar-prod

hashicorp-vault-sonar-prod Bot commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Comment thread java-checks/src/main/java/org/sonar/java/checks/StringIsEmptyCheck.java Outdated
asya-vorobeva and others added 2 commits October 15, 2026 11:24
… comparisons

"strip()" removes exactly the characters "isBlank()" treats as white space,
so the rewrite is equivalent. "trim()" removes every character up to U+0020
instead, so it disagrees on control characters below U+0020 and on Unicode
white space above it; those cases keep the suggestion but the issue message
and the quick-fix label state what the replacement changes.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

@lijun-chen-sonarsource lijun-chen-sonarsource left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM!

Left one test suggestion, if that makes sense.

Below Java 11 neither "strip()" nor "trim()" may be rewritten to
"isBlank()", since that method does not exist yet. Pin that the
comparison is still reported as an emptiness check on the trimmed
string, with an "isEmpty()" quick fix.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@sonarqube-next

Copy link
Copy Markdown
Contributor

@gitar-bot

gitar-bot Bot commented Oct 16, 2026 •

Copy link
Copy Markdown
Code Review ✅ Approved 1 closed / 1 findings

🟡 Medium risk · The new isBlank() quick fixes for trim/strip checks may change whitespace semantics, while expanded zero-literal parsing also affects analyzer findings.

Improves rule S7158 to detect zero literals written in any base and to suggest isBlank() for strip().length() and trim().length() comparisons. No open issues found.

✅ 1 closed
Closed: Cross-PR: S7158 and S9432 both flag trim().length() with conflicting advice

📄 java-checks/src/main/java/org/sonar/java/checks/StringIsEmptyCheck.java:132-143 📄 java-checks/src/main/java/org/sonar/java/checks/StringIsEmptyCheck.java:188-196 📄 java-checks-test-sources/default/src/main/java/checks/StringIsBlankCheckSample.java:9-23 📄 java-checks/src/main/java/org/sonar/java/checks/StringIsEmptyCheck.java:59-63 📄 java-checks-test-sources/default/src/main/java/checks/StringIsBlankCheckSample.java:9-11 📄 java-checks-test-sources/default/src/main/java/checks/StringIsBlankCheckSample.java:72-75 📄 java-checks/src/main/java/org/sonar/java/checks/StringIsEmptyCheck.java:143-157 📄 java-checks/src/main/java/org/sonar/java/checks/StringIsEmptyCheck.java:205-213
Breaks when this PR and #6332 are both merged, and both S7158 and S9432 are enabled on code analyzed as Java 11 or later that contains s.trim().length() == 0 (or a related form).

How

  1. This PR's StringIsEmptyCheck reports trim().length() compared with 0 or 1, and its quick fix rewrites the comparison to isBlank() as a plain replacement (java-checks/src/main/java/org/sonar/java/checks/StringIsEmptyCheck.java:133-143).
  2. In SONARJAVA-7157: Implemented rule S9432 Strings should not be trimmed or stripped just to check if they are blank #6332, S9432Check.visitNode reports the same expressions at the trim() call (java-checks/src/main/java/org/sonar/java/checks/S9432Check.java:77). Its message says to use isBlank() only if Unicode whitespace should count as blank and control characters should not.
  3. Each such line therefore gets two issues, placed at different locations and with contradicting messages. The S7158 quick fix silently changes behavior in exactly the case S9432 warns about.
  4. For s.strip().length() == 0 and (s.trim()).length() == 0, S7158 suggests isEmpty() while S9432 suggests removing the strip/trim, so the two rules give opposite advice.

Fix

Compared b049767 with #6332 at 6a682bc

Verification Rules 1/1 met Repos 3 inspected · PRs 1 inspected

Functional validation No results

Rules 1 passed 2 not applicable

✅ Repository license compliance

Broader impact Repos 3 inspected PRs 1 inspected
Cross-repo 3 repositories inspected 0 open impacts

SonarSource/sonarlint-vscode Inspected
SonarSource/sonarlint-language-server Inspected
SonarSource/sonarlint-intellij Inspected

Cross-PR 1 PR inspected 0 open impacts

PR #6332: SONARJAVA-7157: Implemented rule S9432 Strings should not be trimmed or stripped just to check if they are blank Inspected

Auto-approval Not enabled

Set up

For more verification details, comment gitar display:verbose.

Options

Auto-apply is off → Gitar will not commit updates to this branch.
Display: compact → Showing compact verification results.

Comment with these commands to change the behavior for this request:

Auto-apply Compact
gitar auto-apply:on         
gitar display:verbose         

Was this helpful? React with 👍 / 👎 | Gitar

@asya-vorobeva
asya-vorobeva merged commit 47dfacd into master Oct 16, 2026
18 checks passed
@asya-vorobeva
asya-vorobeva deleted the asya/improve-s7158 branch October 16, 2026 10:50
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