File tree Expand file tree Collapse file tree 3 files changed +67
-1
lines changed
sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java Expand file tree Collapse file tree 3 files changed +67
-1
lines changed Original file line number Diff line number Diff line change 1+ < p > This rule raises an issue when increment (< code > ++</ code > ) or decrement (< code > --</ code > ) operators are used with floating-point variables.</ p >
2+ < h2 > Why is this an issue?</ h2 >
3+ < p > Increment and decrement operators (< code > ++</ code > and < code > --</ code > ) shouldn’t be used with floating-point variables (like < code > float</ code > or
4+ < code > double</ code > ). While the language allows it, the usage is not idiomatic, and most developers intuitively expect < code > x++</ code > to apply to
5+ integer types. Using it on a float violates this common expectation and can lead to misleading code.</ p >
6+ < h3 > What is the potential impact?</ h3 >
7+ < p > Floating-point arithmetic has some non-intuitive properties, which can lead to unexpected bugs. For example, the following loop will not terminate.
8+ 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
9+ insignificant and the increment operation does nothing:</ p >
10+ < pre >
11+ for (float x = 16_000_000; x < 17_000_000; x++) {
12+ // ...
13+ }
14+ // The loop does not terminate
15+ </ pre >
16+ < 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 >
17+ < pre >
18+ for (int x = 16_000_000; x < 17_000_000; x++) {
19+ // ...
20+ }
21+ // This loop terminates.
22+ </ pre >
23+ < h2 > How to fix it</ h2 >
24+ < p > Using the compound assignment operators (< code > +=</ code > and < code > -=</ code > ) makes the intent clearer and avoids the surprising use of
25+ < code > ++</ code > and < code > --</ code > on floating-point types.</ p >
26+ < h3 > Code examples</ h3 >
27+ < h4 > Noncompliant code example</ h4 >
28+ < pre >
29+ float x = 0f;
30+ double y = 1.0;
31+
32+ x++;
33+ y--;
34+ </ pre >
35+ < h4 > Compliant solution</ h4 >
36+ < pre >
37+ float x = 0f;
38+ double y = 1.0;
39+
40+ x += 1.0;
41+ y -= 1.0;
42+ </ pre >
43+
Original file line number Diff line number Diff line change 1+ {
2+ "title" : " Increment and decrement operators (++\/ --) should not be used with floating point variables" ,
3+ "type" : " CODE_SMELL" ,
4+ "status" : " ready" ,
5+ "remediation" : {
6+ "func" : " Constant\/ Issue" ,
7+ "constantCost" : " 5min"
8+ },
9+ "tags" : [],
10+ "defaultSeverity" : " Major" ,
11+ "ruleSpecification" : " RSPEC-8346" ,
12+ "sqKey" : " S8346" ,
13+ "scope" : " All" ,
14+ "quickfix" : " targeted" ,
15+ "code" : {
16+ "impacts" : {
17+ "MAINTAINABILITY" : " HIGH" ,
18+ "RELIABILITY" : " MEDIUM"
19+ },
20+ "attribute" : " CONVENTIONAL"
21+ }
22+ }
Original file line number Diff line number Diff line change 516516 " S7479" ,
517517 " S7481" ,
518518 " S7482" ,
519- " S7629"
519+ " S7629" ,
520+ " S8346"
520521 ]
521522}
You can’t perform that action at this time.
0 commit comments