Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,23 @@ <h2>Why is this an issue?</h2>
<p>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.</p>
<h3>Exceptions</h3>
<p>Casts from <code>int</code> to <code>float</code>, from <code>long</code> to <code>float</code>, and from <code>long</code> to <code>double</code>
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.</p>
<p>Compile-time constants between -2<sup>24</sup> and 2<sup>24</sup> (inclusive) for <code>float</code>, and between -2<sup>53</sup> and
2<sup>53</sup> (inclusive) for <code>double</code>, do not lose precision during conversion. Redundant casts of these constants are still
reported.</p>
<pre>
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
}
}
</pre>
<p>Casting may be required to distinguish the method to call in the case of overloading:</p>
<pre>
class A {}
Expand Down Expand Up @@ -62,4 +79,8 @@ <h3>Documentation</h3>
<li>Wikipedia - <a href="https://en.wikipedia.org/wiki/Strong_and_weak_typing">Strong and Weak Typing</a></li>
<li>Wikipedia - <a href="https://en.wikipedia.org/wiki/Polymorphism_(computer_science)"> Polymorphism (Computer Science)</a></li>
</ul>
<h3>Related rules</h3>
<ul>
<li>{rule:java:S9395} - Numeric values should be explicitly cast when assigned to narrower types</li>
</ul>

Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ <h4>Compliant solution</h4>
<h2>Resources</h2>
<h3>Documentation</h3>
<ul>
<li><a
href="https://owasp.org/www-project-web-security-testing-guide/stable/4-Web_Application_Security_Testing/01-Information_Gathering/08-Fingerprint_Web_Application_Framework.html">OWASP Testing Guide - OTG-INFO-008</a> - Fingerprint Web Application Framework</li>
<li>OWASP Testing Guide - <a
href="https://wstg.owasp.org/v4.2/4-Web_Application_Security_Testing/01-Information_Gathering/08-Fingerprint_Web_Application_Framework">OTG-INFO-008
- Fingerprint Web Application Framework</a></li>
</ul>
<h3>Standards</h3>
<ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ <h2>Exceptions</h2>
the method without any transaction context, so there is no transaction to roll back.</li>
<li><code>@Transactional(readOnly = true)</code> - Read-only transactions do not perform write operations, so there is no risk of committing partial
or inconsistent data.</li>
<li>Methods that throw all their checked exceptions before any write operation (such as <code>save</code>, <code>update</code>, <code>delete</code>,
<code>persist</code>, <code>merge</code>, <code>flush</code> or <code>insert</code>). In that case, the method has not modified any data when the
exception occurs, so there is nothing to roll back.</li>
</ul>
<h2>How to fix it in Spring</h2>
<p>Explicitly specify <code>rollbackFor</code> to include the checked exceptions that should trigger a rollback. This is the most common fix, as most
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ <h2>How to fix it</h2>
<li>If the precision loss is acceptable, add an explicit cast to document that it is intentional.</li>
</ul>
<p>Simply adding a cast may hide a bug, which is why no automatic fix is proposed.</p>
<p>For integer arithmetic expressions such as <code>a / b</code> 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.</p>
<p>For integer arithmetic expressions such as <code>a / b</code> 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.</p>
<h3>Code examples</h3>
<h4>Noncompliant code example</h4>
<pre data-diff-id="1" data-diff-type="noncompliant">
Expand Down
2 changes: 1 addition & 1 deletion sonarpedia.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading