Create rule S7158: String.isEmpty() should be used to test for emptiness (#4493)

This commit is contained in:
github-actions[bot] 2024-11-14 17:03:15 +01:00 committed by GitHub
parent 423514e941
commit 41e6f81392
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 54 additions and 0 deletions

View File

@ -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"
}
}

View File

@ -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]

View File

@ -0,0 +1,2 @@
{
}