Skip to content

Conversation

@github-actions
Copy link
Contributor

@github-actions github-actions bot commented Dec 3, 2025

You can preview this rule here (updated a few minutes after each push).

Review

A dedicated reviewer checked the rule description successfully for:

  • logical errors and incorrect information
  • information gaps and missing content
  • text style and tone
  • PR summary and labels follow the guidelines

Description

Increment and decrement operators (++ and --) shouldn't be used with floating-point variables (like float or double). While the language allows it, the usage is not idiomatic and most developers intuitively expect x++ to apply to integer types. Using it on a float violates this common expectation and can lead to misleading code.

In particular floating point arithmetic has some non-intuitive properties, which can lead to unexpected bugs. For example, the following loop will not terminate:

for (float x = 16_000_000; x < 17_000_000; x++) {
  // ...
}
// The loop does not terminate

This happens because float has only 24 bits of precision and once a number gets large enough, adding 1.0 becomes insignificant.

The problem would not occur if int is used instead of float, even though both bytes occupy 32 bits.

for (int x = 16_000_000; x < 17_000_000; x++) {
  // ...
}
// This loop terminates.

Noncompliant code

x++;
y--;

Compliant code

x += 1.0;
y -= 1.0;

@github-actions github-actions bot added the java label Dec 3, 2025
@rombirli rombirli changed the title Create rule S8346 Create rule S8346: Increment and decrement operators (++/--) should not be used with floating point variables. Dec 3, 2025
@rombirli rombirli marked this pull request as ready for review December 3, 2025 07:51
@rombirli rombirli force-pushed the rule/add-RSPEC-S8346 branch from efbcbb6 to 0f80326 Compare December 3, 2025 13:38
@sonarqube-next
Copy link

sonarqube-next bot commented Dec 3, 2025

Quality Gate passed Quality Gate passed for 'rspec-tools'

Issues
0 New issues
0 Fixed issues
0 Accepted issues

Measures
0 Security Hotspots
0 Dependency risks
No data about Coverage
No data about Duplication

See analysis details on SonarQube

@sonarqube-next
Copy link

sonarqube-next bot commented Dec 3, 2025

Quality Gate passed Quality Gate passed for 'rspec-frontend'

Issues
0 New issues
0 Fixed issues
0 Accepted issues

Measures
0 Security Hotspots
0 Dependency risks
No data about Coverage
No data about Duplication

See analysis details on SonarQube

----

This happens because `float` has only 24 bits of precision (mantissa) and once a number gets large enough, adding `1.0` becomes insignificant.
The increment operation effectively does nothing.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's merge it with the previous sentence to improve flow. For example, "adding 1.0 becomes insignificant and the operation does nothing"

// This loop terminates.
----

Using the compound assignment operators (`{plus}=` and `-=`) makes the intent clearer and avoids the surprising use of `{plus}{plus}` and `--` on floating-point types.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about we move this sentence to "How to fix it"?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants