From d6743d0ee96d5172656aae6c002f9ec6c8ebd25f Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Sun, 23 Oct 2022 22:06:33 +0200 Subject: [PATCH] doc: Update docs/HANDLED_RULES.md (#925) Co-authored-by: github-actions[bot] --- docs/HANDLED_RULES.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/HANDLED_RULES.md b/docs/HANDLED_RULES.md index 3982b8614..be261eb4e 100644 --- a/docs/HANDLED_RULES.md +++ b/docs/HANDLED_RULES.md @@ -388,14 +388,15 @@ Example: #### Strings and Boxed types should be compared using "equals()" ([Sonar Rule 4973](https://rules.sonarsource.com/java/RSPEC-4973)) Any comparison of strings or boxed types using `==` or `!=` is replaced by `equals`. +By default, we use `equals` on the left operand, so we add a null check before comparison. Example: ```diff - if (firstName == lastName) // Noncompliant -+ if (firstName.equals(lastName)) ++ if (firstName != null && firstName.equals(lastName)) ... - return b != a; // Noncompliant -+ return !b.equals(a); ++ return b != null && !b.equals(a); ``` Check out an accepted PR in [Apache Sling Discovery](https://github.com/apache/sling-org-apache-sling-discovery-impl/pull/1) that repairs one CompareStringsBoxedTypesWithEquals violation.