diff --git a/rules/S7158/java/metadata.json b/rules/S7158/java/metadata.json new file mode 100644 index 00000000000..7142ff20cfb --- /dev/null +++ b/rules/S7158/java/metadata.json @@ -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" + } +} diff --git a/rules/S7158/java/rule.adoc b/rules/S7158/java/rule.adoc new file mode 100644 index 00000000000..7eb6bab3efe --- /dev/null +++ b/rules/S7158/java/rule.adoc @@ -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] diff --git a/rules/S7158/metadata.json b/rules/S7158/metadata.json new file mode 100644 index 00000000000..2c63c085104 --- /dev/null +++ b/rules/S7158/metadata.json @@ -0,0 +1,2 @@ +{ +}