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
@@ -0,0 +1,6 @@
{
"ruleKey": "S8346",
"hasTruePositives": true,
"falseNegatives": 0,
"falsePositives": 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"commons-beanutils:commons-beanutils:src/main/java/org/apache/commons/beanutils2/MethodUtils.java": [
1136
]
}
5 changes: 5 additions & 0 deletions its/ruling/src/test/resources/sonar-server/java-S8346.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"org.sonarsource.sonarqube:sonar-server:src/test/java/org/sonar/server/measure/ws/ComponentTreeSortTest.java": [
85
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* SonarQube Java
* Copyright (C) 2012-2025 SonarSource Sàrl
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the Sonar Source-Available License for more details.
*
* You should have received a copy of the Sonar Source-Available License
* along with this program; if not, see https://sonarsource.com/license/ssal/
*/
package org.sonar.java.checks;

import java.util.List;
import java.util.Optional;
import org.sonar.check.Rule;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.semantic.Type;
import org.sonar.plugins.java.api.semantic.Type.Primitives;
import org.sonar.plugins.java.api.tree.IdentifierTree;
import org.sonar.plugins.java.api.tree.Tree;
import org.sonar.plugins.java.api.tree.UnaryExpressionTree;

@Rule(key = "S8346")
public class IncDecOnFloatingPointCheck extends IssuableSubscriptionVisitor {

@Override
public List<Tree.Kind> nodesToVisit() {
return List.of(
Tree.Kind.POSTFIX_INCREMENT,
Tree.Kind.PREFIX_INCREMENT,
Tree.Kind.POSTFIX_DECREMENT,
Tree.Kind.PREFIX_DECREMENT
);
}

@Override
public void visitNode(Tree tree) {
Optional.of(tree)
.filter(UnaryExpressionTree.class::isInstance)
.map(UnaryExpressionTree.class::cast)
.filter(unaryExpr ->
unaryExpr.expression() instanceof IdentifierTree identifierTree
&& isFloatingPoint(identifierTree.symbolType())
)
.ifPresent(unaryExpr -> reportIssue(
unaryExpr,
"%s operator (%s) should not be used with floating point variables".formatted(
isIncrement(unaryExpr) ? "Increment" : "Decrement",
unaryExpr.operatorToken().text()
)
));
}


private static boolean isFloatingPoint(Type type) {
return type.isPrimitive(Primitives.FLOAT) || type.isPrimitive(Primitives.DOUBLE);
}

private static boolean isIncrement(UnaryExpressionTree unaryExp) {
return unaryExp.is(
Tree.Kind.PREFIX_INCREMENT,
Tree.Kind.POSTFIX_INCREMENT
);
}
}
100 changes: 100 additions & 0 deletions java-checks/src/test/files/checks/IncDecOnFloatingPointCheck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
class A {
void specNonCompliantExamples() {
for (float i = 16_000_000; i < 17_000_000; i++) { // Noncompliant {{Increment operator (++) should not be used with floating point variables}}
// ^^^
// ...
}


float x = 0f;
double y = 1.0;

x++; // Noncompliant {{Increment operator (++) should not be used with floating point variables}}
// ^^^
y--; // Noncompliant {{Decrement operator (--) should not be used with floating point variables}}
// ^^^
}

void specCompliantExamples() {
for (int i = 16_000_000; i < 17_000_000; i++) { // Compliant
// ...
}

float x = 0f;
double y = 1.0;

x += 1.0; // Compliant
y -= 1.0; // Compliant
}

void floatIsNotCompliant() {
float y = 0.1f;
y++; // Noncompliant {{Increment operator (++) should not be used with floating point variables}}
// ^^^
++y; // Noncompliant {{Increment operator (++) should not be used with floating point variables}}
// ^^^
y--; // Noncompliant {{Decrement operator (--) should not be used with floating point variables}}
// ^^^
--y; // Noncompliant {{Decrement operator (--) should not be used with floating point variables}}
// ^^^
}

void doubleIsNotCompliant() {
double y = 0.1;
y++; // Noncompliant {{Increment operator (++) should not be used with floating point variables}}
// ^^^
++y; // Noncompliant {{Increment operator (++) should not be used with floating point variables}}
// ^^^
y--; // Noncompliant {{Decrement operator (--) should not be used with floating point variables}}
// ^^^
--y; // Noncompliant {{Decrement operator (--) should not be used with floating point variables}}
// ^^^
}

void intIsCompliant() {
int z = 0;
z++; // Compliant
++z; // Compliant
z--; // Compliant
--z; // Compliant
}

void longIsCompliant() {
long w = 0L;
w++; // Compliant
++w; // Compliant
w--; // Compliant
--w; // Compliant
}

void charIsCompliant() {
char c = 'a';
c++; // Compliant
++c; // Compliant
c--; // Compliant
--c; // Compliant
}

void otherOperatorsNotAffected() {
float f = 1f;
// test unary operator -
float g = -f; // compliant
// test binary operators just in case
float h = f + 1f; // compliant
}

// variable shadowing test
private double d = 3.4;

void variableShadowingTestCompliant() {
for (int d = 0; d < 10; d++) { // Compliant
}
}
// variable shadowing test
private int i = 3;

void variableShadowingTestCompliant() {
for (float i = 0; i < 10; i++) { // Noncompliant {{Increment operator (++) should not be used with floating point variables}}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* SonarQube Java
* Copyright (C) 2012-2025 SonarSource Sàrl
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the Sonar Source-Available License for more details.
*
* You should have received a copy of the Sonar Source-Available License
* along with this program; if not, see https://sonarsource.com/license/ssal/
*/
package org.sonar.java.checks;

import org.junit.jupiter.api.Test;
import org.sonar.java.checks.verifier.CheckVerifier;

class IncDecOnFloatingPointCheckTest {

@Test
void test() {
CheckVerifier.newVerifier()
.onFile("src/test/files/checks/IncDecOnFloatingPointCheck.java")
.withCheck(new IncDecOnFloatingPointCheck())
.verifyIssues();
}

@Test
void testNoSemantic() {
CheckVerifier.newVerifier()
.withoutSemantic()
.onFile("src/test/files/checks/IncDecOnFloatingPointCheck.java")
.withCheck(new IncDecOnFloatingPointCheck())
.verifyIssues();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<p>This rule raises an issue when increment (<code>++</code>) or decrement (<code>--</code>) operators are used with floating-point variables.</p>
<h2>Why is this an issue?</h2>
<p>Increment and decrement operators (<code>++</code> and <code>--</code>) shouldn’t be used with floating-point variables (like <code>float</code> or
<code>double</code>). While the language allows it, the usage is not idiomatic, and most developers intuitively expect <code>x++</code> to apply to
integer types. Using it on a float violates this common expectation and can lead to misleading code.</p>
<h3>What is the potential impact?</h3>
<p>Floating-point arithmetic has some non-intuitive properties, which can lead to unexpected bugs. For example, the following loop will not terminate.
This happens because <code>float</code> has only 24 bits of precision (mantissa) and once a number gets large enough, adding <code>1.0</code> becomes
insignificant and the increment operation does nothing:</p>
<pre>
for (float x = 16_000_000; x &lt; 17_000_000; x++) {
// ...
}
// The loop does not terminate
</pre>
<p>The problem would not occur if <code>int</code> is used instead of <code>float</code>, even though both types occupy 32 bits:</p>
<pre>
for (int x = 16_000_000; x &lt; 17_000_000; x++) {
// ...
}
// This loop terminates.
</pre>
<h2>How to fix it</h2>
<p>Using the compound assignment operators (<code>+=</code> and <code>-=</code>) makes the intent clearer and avoids the surprising use of
<code>++</code> and <code>--</code> on floating-point types.</p>
<h3>Code examples</h3>
<h4>Noncompliant code example</h4>
<pre>
float x = 0f;
double y = 1.0;

x++;
y--;
</pre>
<h4>Compliant solution</h4>
<pre>
float x = 0f;
double y = 1.0;

x += 1.0;
y -= 1.0;
</pre>

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"title": "Increment and decrement operators (++\/--) should not be used with floating point variables",
"type": "CODE_SMELL",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-8346",
"sqKey": "S8346",
"scope": "All",
"quickfix": "targeted",
"code": {
"impacts": {
"MAINTAINABILITY": "HIGH",
"RELIABILITY": "MEDIUM"
},
"attribute": "CONVENTIONAL"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@
"S7479",
"S7481",
"S7482",
"S7629"
"S7629",
"S8346"
]
}
Loading