Create rule S7158: String.isEmpty() should be used to test for emptiness (#4493)
This commit is contained in:
parent
423514e941
commit
41e6f81392
24
rules/S7158/java/metadata.json
Normal file
24
rules/S7158/java/metadata.json
Normal 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"
|
||||
}
|
||||
}
|
28
rules/S7158/java/rule.adoc
Normal file
28
rules/S7158/java/rule.adoc
Normal 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]
|
2
rules/S7158/metadata.json
Normal file
2
rules/S7158/metadata.json
Normal file
@ -0,0 +1,2 @@
|
||||
{
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user