From bfae2ece22ff065b0682384765b5f3dfdfb707da Mon Sep 17 00:00:00 2001 From: tomasz-tylenda-sonarsource Date: Tue, 12 Nov 2024 13:18:49 +0000 Subject: [PATCH 1/5] Create rule S7158 --- rules/S7158/java/metadata.json | 25 +++++++++++++++++++ rules/S7158/java/rule.adoc | 44 ++++++++++++++++++++++++++++++++++ rules/S7158/metadata.json | 2 ++ 3 files changed, 71 insertions(+) create mode 100644 rules/S7158/java/metadata.json create mode 100644 rules/S7158/java/rule.adoc create mode 100644 rules/S7158/metadata.json diff --git a/rules/S7158/java/metadata.json b/rules/S7158/java/metadata.json new file mode 100644 index 00000000000..b8db65b42cd --- /dev/null +++ b/rules/S7158/java/metadata.json @@ -0,0 +1,25 @@ +{ + "title": "FIXME", + "type": "CODE_SMELL", + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "5min" + }, + "tags": [ + ], + "defaultSeverity": "Major", + "ruleSpecification": "RSPEC-7158", + "sqKey": "S7158", + "scope": "All", + "defaultQualityProfiles": ["Sonar way"], + "quickfix": "unknown", + "code": { + "impacts": { + "MAINTAINABILITY": "HIGH", + "RELIABILITY": "MEDIUM", + "SECURITY": "LOW" + }, + "attribute": "CONVENTIONAL" + } +} diff --git a/rules/S7158/java/rule.adoc b/rules/S7158/java/rule.adoc new file mode 100644 index 00000000000..4172043c9d3 --- /dev/null +++ b/rules/S7158/java/rule.adoc @@ -0,0 +1,44 @@ +FIXME: add a description + +// If you want to factorize the description uncomment the following line and create the file. +//include::../description.adoc[] + +== Why is this an issue? + +FIXME: remove the unused optional headers (that are commented out) + +//=== What is the potential impact? + +== How to fix it +//== How to fix it in FRAMEWORK NAME + +=== Code examples + +==== Noncompliant code example + +[source,java,diff-id=1,diff-type=noncompliant] +---- +FIXME +---- + +==== Compliant solution + +[source,java,diff-id=1,diff-type=compliant] +---- +FIXME +---- + +//=== How does this work? + +//=== Pitfalls + +//=== Going the extra mile + + +//== Resources +//=== Documentation +//=== Articles & blog posts +//=== Conference presentations +//=== Standards +//=== External coding guidelines +//=== Benchmarks 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 @@ +{ +} From fa1b7a7662548ada5583646fab41a2503cffe57c Mon Sep 17 00:00:00 2001 From: tomasz-tylenda-sonarsource Date: Tue, 12 Nov 2024 13:18:49 +0000 Subject: [PATCH 2/5] Create rule S7158: String.isEmpty() should be used to test for emptiness. --- rules/S7158/java/metadata.json | 21 +++++++++-------- rules/S7158/java/rule.adoc | 41 +++++++--------------------------- 2 files changed, 18 insertions(+), 44 deletions(-) diff --git a/rules/S7158/java/metadata.json b/rules/S7158/java/metadata.json index b8db65b42cd..7142ff20cfb 100644 --- a/rules/S7158/java/metadata.json +++ b/rules/S7158/java/metadata.json @@ -1,25 +1,24 @@ { - "title": "FIXME", + "title": "\"String.isEmpty()\" should be used to test for emptiness", "type": "CODE_SMELL", "status": "ready", "remediation": { "func": "Constant\/Issue", - "constantCost": "5min" + "constantCost": "2min" }, - "tags": [ - ], - "defaultSeverity": "Major", + "tags": [], + "defaultSeverity": "Minor", "ruleSpecification": "RSPEC-7158", "sqKey": "S7158", "scope": "All", - "defaultQualityProfiles": ["Sonar way"], - "quickfix": "unknown", + "defaultQualityProfiles": [ + "Sonar way" + ], + "quickfix": "targeted", "code": { "impacts": { - "MAINTAINABILITY": "HIGH", - "RELIABILITY": "MEDIUM", - "SECURITY": "LOW" + "MAINTAINABILITY": "LOW" }, - "attribute": "CONVENTIONAL" + "attribute": "CLEAR" } } diff --git a/rules/S7158/java/rule.adoc b/rules/S7158/java/rule.adoc index 4172043c9d3..75b6f9eb85e 100644 --- a/rules/S7158/java/rule.adoc +++ b/rules/S7158/java/rule.adoc @@ -1,44 +1,19 @@ -FIXME: add a description - -// If you want to factorize the description uncomment the following line and create the file. -//include::../description.adoc[] - == Why is this an issue? -FIXME: remove the unused optional headers (that are commented out) - -//=== What is the potential impact? - -== How to fix it -//== How to fix it in FRAMEWORK NAME - -=== Code examples - -==== Noncompliant code example +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. [source,java,diff-id=1,diff-type=noncompliant] ---- -FIXME +if ("string".length() == 0) { /* … */ } // Noncompliant + +if ("string".length() > 0) { /* … */ } // Noncompliant ---- -==== Compliant solution +Prefer `String.isEmpty()` for clarity. [source,java,diff-id=1,diff-type=compliant] ---- -FIXME ----- - -//=== How does this work? - -//=== Pitfalls +if ("string".isEmpty()){ /* … */ } -//=== Going the extra mile - - -//== Resources -//=== Documentation -//=== Articles & blog posts -//=== Conference presentations -//=== Standards -//=== External coding guidelines -//=== Benchmarks +if (!"string".isEmpty()){ /* … */ } +---- From 7af9c1c6fb5ac40b86160ca57f5bd27a0571257b Mon Sep 17 00:00:00 2001 From: tomasz-tylenda-sonarsource Date: Thu, 14 Nov 2024 16:19:13 +0100 Subject: [PATCH 3/5] Update rules/S7158/java/rule.adoc Co-authored-by: leonardo-pilastri-sonarsource <115481625+leonardo-pilastri-sonarsource@users.noreply.github.com> --- rules/S7158/java/rule.adoc | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/rules/S7158/java/rule.adoc b/rules/S7158/java/rule.adoc index 75b6f9eb85e..a28e6e50c8c 100644 --- a/rules/S7158/java/rule.adoc +++ b/rules/S7158/java/rule.adoc @@ -2,6 +2,11 @@ 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 From 869da6c04a646696654406929e5caf1b0946ba60 Mon Sep 17 00:00:00 2001 From: tomasz-tylenda-sonarsource Date: Thu, 14 Nov 2024 16:19:27 +0100 Subject: [PATCH 4/5] Update rules/S7158/java/rule.adoc Co-authored-by: leonardo-pilastri-sonarsource <115481625+leonardo-pilastri-sonarsource@users.noreply.github.com> --- rules/S7158/java/rule.adoc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/rules/S7158/java/rule.adoc b/rules/S7158/java/rule.adoc index a28e6e50c8c..37b9b097a5c 100644 --- a/rules/S7158/java/rule.adoc +++ b/rules/S7158/java/rule.adoc @@ -14,8 +14,7 @@ if ("string".length() == 0) { /* … */ } // Noncompliant if ("string".length() > 0) { /* … */ } // Noncompliant ---- -Prefer `String.isEmpty()` for clarity. - +==== Compliant solution [source,java,diff-id=1,diff-type=compliant] ---- if ("string".isEmpty()){ /* … */ } From a08e951740c92c401d83c472ab07f92e8dbe93ac Mon Sep 17 00:00:00 2001 From: tomasz-tylenda-sonarsource Date: Thu, 14 Nov 2024 16:20:19 +0100 Subject: [PATCH 5/5] Update rules/S7158/java/rule.adoc Co-authored-by: leonardo-pilastri-sonarsource <115481625+leonardo-pilastri-sonarsource@users.noreply.github.com> --- rules/S7158/java/rule.adoc | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/rules/S7158/java/rule.adoc b/rules/S7158/java/rule.adoc index 37b9b097a5c..7eb6bab3efe 100644 --- a/rules/S7158/java/rule.adoc +++ b/rules/S7158/java/rule.adoc @@ -21,3 +21,8 @@ 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]