From fdc023c85c824c50c2c49b2d9ef4d533b603f0fd Mon Sep 17 00:00:00 2001 From: "erwan.serandour" Date: Mon, 16 Jun 2025 16:03:13 +0200 Subject: [PATCH 1/4] SONARJAVA-5602: Modify rule S3033, add CCT attributes to the metadata --- rules/S7158/java/rule.adoc | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/rules/S7158/java/rule.adoc b/rules/S7158/java/rule.adoc index 7eb6bab3efe..d5ac55ee073 100644 --- a/rules/S7158/java/rule.adoc +++ b/rules/S7158/java/rule.adoc @@ -1,6 +1,7 @@ == 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. +This preference for `isEmpty()` applies to all `CharSequence` objects, such as `StringBuilder` and `StringBuffer`. == How to fix it @@ -13,6 +14,12 @@ if ("string".length() == 0) { /* … */ } // Noncompliant if ("string".length() > 0) { /* … */ } // Noncompliant ---- +[source,java,diff-id=2,diff-type=noncompliant] +---- +StringBuilder sb = new StringBuilder(); +... +if (sb.length() == 0) { /* … */ } // Noncompliant +---- ==== Compliant solution [source,java,diff-id=1,diff-type=compliant] @@ -21,6 +28,12 @@ if ("string".isEmpty()){ /* … */ } if (!"string".isEmpty()){ /* … */ } ---- +[source,java,diff-id=2,diff-type=compliant] +---- +StringBuilder sb = new StringBuilder(); +... +if (sb.isEmpty()) { /* … */ } +---- == Resources === Documentation From cefd2cf3bcfb013189e4896c16df7234912d084d Mon Sep 17 00:00:00 2001 From: "erwan.serandour" Date: Mon, 16 Jun 2025 16:07:03 +0200 Subject: [PATCH 2/4] fix typo --- rules/S7158/java/rule.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rules/S7158/java/rule.adoc b/rules/S7158/java/rule.adoc index d5ac55ee073..a14f5fa6760 100644 --- a/rules/S7158/java/rule.adoc +++ b/rules/S7158/java/rule.adoc @@ -1,7 +1,7 @@ == 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. -This preference for `isEmpty()` applies to all `CharSequence` objects, such as `StringBuilder` and `StringBuffer`. +This preference for `isEmpty()` applies to all `CharSequence` objects, such as `StringBuilder` and `StringBuffer` . == How to fix it From bbc3f9cdb45f7f7f44a814af1d630ac078089bd8 Mon Sep 17 00:00:00 2001 From: "erwan.serandour" Date: Mon, 16 Jun 2025 16:07:10 +0200 Subject: [PATCH 3/4] fix typo --- rules/S7158/java/rule.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rules/S7158/java/rule.adoc b/rules/S7158/java/rule.adoc index a14f5fa6760..d5ac55ee073 100644 --- a/rules/S7158/java/rule.adoc +++ b/rules/S7158/java/rule.adoc @@ -1,7 +1,7 @@ == 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. -This preference for `isEmpty()` applies to all `CharSequence` objects, such as `StringBuilder` and `StringBuffer` . +This preference for `isEmpty()` applies to all `CharSequence` objects, such as `StringBuilder` and `StringBuffer`. == How to fix it From 3c5f481b04bef36527814f2790fd0891635e7030 Mon Sep 17 00:00:00 2001 From: "erwan.serandour" Date: Tue, 17 Jun 2025 12:02:30 +0200 Subject: [PATCH 4/4] update after code review --- rules/S7158/java/rule.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rules/S7158/java/rule.adoc b/rules/S7158/java/rule.adoc index d5ac55ee073..afec24a5a7b 100644 --- a/rules/S7158/java/rule.adoc +++ b/rules/S7158/java/rule.adoc @@ -1,7 +1,7 @@ == 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. -This preference for `isEmpty()` applies to all `CharSequence` objects, such as `StringBuilder` and `StringBuffer`. +This preference for `isEmpty()` extends to all `CharSequence` objects, including `StringBuilder` and `StringBuffer`. == How to fix it