Skip to content

Commit

Permalink
Create rule S7158: String.isEmpty() should be used to test for emptin…
Browse files Browse the repository at this point in the history
…ess (#4493)
  • Loading branch information
github-actions[bot] authored Nov 14, 2024
1 parent 423514e commit 41e6f81
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
24 changes: 24 additions & 0 deletions rules/S7158/java/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"title": "\"String.isEmpty()\" should be used to test for emptiness",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "2min"
},
"tags": [],
"defaultSeverity": "Minor",
"ruleSpecification": "RSPEC-7158",
"sqKey": "S7158",
"scope": "All",
"defaultQualityProfiles": [
"Sonar way"
],
"quickfix": "targeted",
"code": {
"impacts": {
"MAINTAINABILITY": "LOW"
},
"attribute": "CLEAR"
}
}
28 changes: 28 additions & 0 deletions rules/S7158/java/rule.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
== Why is this an issue?

Calling `String.isEmpty()` clearly communicates the code's intention, which is to test if the string is empty. Using `String.length() == 0` is less direct and makes the code less readable.

== How to fix it

=== Code examples

==== Noncompliant code example
[source,java,diff-id=1,diff-type=noncompliant]
----
if ("string".length() == 0) { /* … */ } // Noncompliant
if ("string".length() > 0) { /* … */ } // Noncompliant
----

==== Compliant solution
[source,java,diff-id=1,diff-type=compliant]
----
if ("string".isEmpty()){ /* … */ }
if (!"string".isEmpty()){ /* … */ }
----

== Resources
=== Documentation

* Java Documentation - https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#isEmpty()[java.lang.String.isEmpty() method]
2 changes: 2 additions & 0 deletions rules/S7158/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}

0 comments on commit 41e6f81

Please sign in to comment.