-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create rule S7158: String.isEmpty() should be used to test for emptin…
…ess (#4493)
- Loading branch information
1 parent
423514e
commit 41e6f81
Showing
3 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{ | ||
} |