-
Notifications
You must be signed in to change notification settings - Fork 707
SONARJAVA-5754 : Increment and decrement operators (++/--) should not be used with floating point variables #5371
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4ce49a6
Started implementing rule S8346 (Rule / Tests)
rombirli b6d7682
S8346 improve test file to test += operators, and all others spec exa…
rombirli d9c7051
Address Tomasz' comments, mostly refactoring, add tests
rombirli c02ad8a
Add rule S8346 documentation
rombirli e67b2df
Use functional prog to avoid low branch coverage, fix test format
rombirli 7757281
Add diff_S8346.json, improve fun prog workaround
rombirli 81d8d20
Added missing test expected issues json file
rombirli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
its/autoscan/src/test/resources/autoscan/diffs/diff_S8346.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "ruleKey": "S8346", | ||
| "hasTruePositives": true, | ||
| "falseNegatives": 0, | ||
| "falsePositives": 0 | ||
| } |
5 changes: 5 additions & 0 deletions
5
its/ruling/src/test/resources/commons-beanutils/java-S8346.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| ] | ||
| } |
71 changes: 71 additions & 0 deletions
71
java-checks/src/main/java/org/sonar/java/checks/IncDecOnFloatingPointCheck.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
100
java-checks/src/test/files/checks/IncDecOnFloatingPointCheck.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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}} | ||
| } | ||
| } | ||
| } |
40 changes: 40 additions & 0 deletions
40
java-checks/src/test/java/org/sonar/java/checks/IncDecOnFloatingPointCheckTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
| } | ||
43 changes: 43 additions & 0 deletions
43
sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java/S8346.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 < 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 < 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> | ||
|
|
22 changes: 22 additions & 0 deletions
22
sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java/S8346.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -516,6 +516,7 @@ | |
| "S7479", | ||
| "S7481", | ||
| "S7482", | ||
| "S7629" | ||
| "S7629", | ||
| "S8346" | ||
| ] | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.