From e17813d11f363a122d8ff8eddaeccf9d1ca9a68b Mon Sep 17 00:00:00 2001 From: aurelien-coet-sonarsource Date: Thu, 29 Oct 2026 13:05:55 +0000 Subject: [PATCH] Update rule metadata --- .../org/sonar/l10n/java/rules/java/S1905.html | 21 +++++++++++++++++++ .../org/sonar/l10n/java/rules/java/S5689.html | 5 +++-- .../org/sonar/l10n/java/rules/java/S8989.html | 3 +++ .../org/sonar/l10n/java/rules/java/S9395.html | 6 +++--- sonarpedia.json | 2 +- 5 files changed, 31 insertions(+), 6 deletions(-) diff --git a/sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java/S1905.html b/sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java/S1905.html index 5a345f543b4..2349efbe34e 100644 --- a/sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java/S1905.html +++ b/sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java/S1905.html @@ -12,6 +12,23 @@

Why is this an issue?

As a result, it’s generally advised to avoid unnecessary casting expressions. Instead, rely on the language’s type system to ensure type safety and code clarity.

Exceptions

+

Casts from int to float, from long to float, and from long to double +can document potential precision loss. Although Java performs these widening conversions implicitly, they can lose precision. This rule preserves +casts that document awareness of potential precision loss, as required by {rule:java:S9395}. The cast does not prevent rounding or preserve the +original value’s precision.

+

Compile-time constants between -224 and 224 (inclusive) for float, and between -253 and +253 (inclusive) for double, do not lose precision during conversion. Redundant casts of these constants are still +reported.

+
+class NumericConversions {
+  void convert(int intValue, long longValue) {
+    float first = (float) intValue; // Compliant
+    float second = (float) longValue; // Compliant
+    double third = (double) longValue; // Compliant
+    float constant = (float) 1; // Noncompliant
+  }
+}
+

Casting may be required to distinguish the method to call in the case of overloading:

 class A {}
@@ -62,4 +79,8 @@ 

Documentation

  • Wikipedia - Strong and Weak Typing
  • Wikipedia - Polymorphism (Computer Science)
  • +

    Related rules

    + diff --git a/sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java/S5689.html b/sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java/S5689.html index 8c6a06c75df..8c4bfa44457 100644 --- a/sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java/S5689.html +++ b/sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java/S5689.html @@ -34,8 +34,9 @@

    Compliant solution

    Resources

    Documentation

    Standards

    How to fix it in Spring

    Explicitly specify rollbackFor to include the checked exceptions that should trigger a rollback. This is the most common fix, as most diff --git a/sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java/S9395.html b/sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java/S9395.html index b562ada93dc..c41ef99df92 100644 --- a/sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java/S9395.html +++ b/sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java/S9395.html @@ -38,9 +38,9 @@

    How to fix it

  • If the precision loss is acceptable, add an explicit cast to document that it is intentional.
  • Simply adding a cast may hide a bug, which is why no automatic fix is proposed.

    -

    For integer arithmetic expressions such as a / b converted to a floating-point type, casting the result would not fix the -integer division or overflow. Instead, one of the operands should be cast before the operation, as described by rule {rule:java:S2184}. Such -expressions are not reported by this rule when {rule:java:S2184} already reports them.

    +

    For integer arithmetic expressions such as a / b converted to a floating-point type, casting the result would not fix the integer +division or overflow. Instead, one of the operands should be cast before the operation, as described by rule {rule:java:S2184}. Such expressions are +not reported by this rule when {rule:java:S2184} already reports them.

    Code examples

    Noncompliant code example

    diff --git a/sonarpedia.json b/sonarpedia.json
    index 60076c6b738..51c21601bd8 100644
    --- a/sonarpedia.json
    +++ b/sonarpedia.json
    @@ -4,7 +4,7 @@
         "JAVA"
       ],
       "profiles-path": "./sonar-java-plugin/src/main/resources/profiles",
    -  "latest-update": "2026-10-22T11:55:36.615101096Z",
    +  "latest-update": "2026-10-29T13:05:53.900556946Z",
       "options": {
         "no-language-in-filenames": true,
         "preserve-filenames": false