Skip to content

Commit d9c7051

Browse files
committed
Address Tomasz' comments, mostly refactoring, add tests
1 parent b6d7682 commit d9c7051

File tree

4 files changed

+139
-89
lines changed

4 files changed

+139
-89
lines changed

java-checks/src/main/java/org/sonar/java/checks/IncDecOnFPCheck.java renamed to java-checks/src/main/java/org/sonar/java/checks/IncDecOnFloatingPointCheck.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,23 @@
1919
import java.util.List;
2020
import org.sonar.check.Rule;
2121
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
22+
import org.sonar.plugins.java.api.semantic.Type;
2223
import org.sonar.plugins.java.api.semantic.Type.Primitives;
2324
import org.sonar.plugins.java.api.tree.IdentifierTree;
2425
import org.sonar.plugins.java.api.tree.Tree;
2526
import org.sonar.plugins.java.api.tree.UnaryExpressionTree;
2627

2728
@Rule(key = "S8346")
28-
public class IncDecOnFPCheck extends IssuableSubscriptionVisitor {
29+
public class IncDecOnFloatingPointCheck extends IssuableSubscriptionVisitor {
2930

3031
@Override
3132
public List<Tree.Kind> nodesToVisit() {
32-
return List.of(Tree.Kind.POSTFIX_INCREMENT, Tree.Kind.PREFIX_INCREMENT, Tree.Kind.POSTFIX_DECREMENT, Tree.Kind.PREFIX_DECREMENT);
33+
return List.of(
34+
Tree.Kind.POSTFIX_INCREMENT,
35+
Tree.Kind.PREFIX_INCREMENT,
36+
Tree.Kind.POSTFIX_DECREMENT,
37+
Tree.Kind.PREFIX_DECREMENT
38+
);
3339
}
3440

3541
@Override
@@ -38,9 +44,26 @@ public void visitNode(Tree tree) {
3844
tree instanceof UnaryExpressionTree unaryExpr &&
3945
unaryExpr.expression() instanceof IdentifierTree identifier &&
4046
// above condition should always be true, just used to pattern match safely
41-
(identifier.symbolType().isPrimitive(Primitives.FLOAT) || identifier.symbolType().isPrimitive(Primitives.DOUBLE))
47+
isFloatingPoint(identifier.symbolType())
4248
) {
43-
reportIssue(unaryExpr, "Increment and decrement operators (++/--) should not be used with floating point variables");
49+
reportIssue(
50+
unaryExpr,
51+
"%s operator (%s) should not be used with floating point variables".formatted(
52+
isIncrement(unaryExpr) ? "Increment" : "Decrement",
53+
unaryExpr.operatorToken().text()
54+
)
55+
);
4456
}
4557
}
58+
59+
private static boolean isFloatingPoint(Type type) {
60+
return type.isPrimitive(Primitives.FLOAT) || type.isPrimitive(Primitives.DOUBLE);
61+
}
62+
63+
private static boolean isIncrement(UnaryExpressionTree unaryExp) {
64+
return unaryExp.is(
65+
Tree.Kind.PREFIX_INCREMENT,
66+
Tree.Kind.POSTFIX_INCREMENT
67+
);
68+
}
4669
}

java-checks/src/test/files/checks/IncDecOnFPCheck.java

Lines changed: 0 additions & 81 deletions
This file was deleted.
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
class A {
2+
void specNonCompliantExamples() {
3+
for (float i = 16_000_000; i < 17_000_000; i++) { // Noncompliant {{Increment operator (++) should not be used with floating point variables}}
4+
// ^^^
5+
// ...
6+
}
7+
8+
9+
float x = 0f;
10+
double y = 1.0;
11+
12+
x++; // Noncompliant {{Increment operator (++) should not be used with floating point variables}}
13+
// ^^^
14+
y--; // Noncompliant {{Decrement operator (--) should not be used with floating point variables}}
15+
// ^^^
16+
}
17+
18+
void specCompliantExamples() {
19+
for (int i = 16_000_000; i < 17_000_000; i++) { // Compliant
20+
// ...
21+
}
22+
23+
float x = 0f;
24+
double y = 1.0;
25+
26+
x += 1.0; // Compliant
27+
y -= 1.0; // Compliant
28+
}
29+
30+
void floatIsNotCompliant() {
31+
float y = 0.1f;
32+
y++; // Noncompliant {{Increment operator (++) should not be used with floating point variables}}
33+
// ^^^
34+
++y; // Noncompliant {{Increment operator (++) should not be used with floating point variables}}
35+
// ^^^
36+
y--; // Noncompliant {{Decrement operator (--) should not be used with floating point variables}}
37+
// ^^^
38+
--y; // Noncompliant {{Decrement operator (--) should not be used with floating point variables}}
39+
// ^^^
40+
}
41+
42+
void doubleIsNotCompliant() {
43+
double y = 0.1;
44+
y++; // Noncompliant {{Increment operator (++) should not be used with floating point variables}}
45+
// ^^^
46+
++y; // Noncompliant {{Increment operator (++) should not be used with floating point variables}}
47+
// ^^^
48+
y--; // Noncompliant {{Decrement operator (--) should not be used with floating point variables}}
49+
// ^^^
50+
--y; // Noncompliant {{Decrement operator (--) should not be used with floating point variables}}
51+
// ^^^
52+
}
53+
54+
void intIsCompliant() {
55+
int z = 0;
56+
z++; // Compliant
57+
++z; // Compliant
58+
z--; // Compliant
59+
--z; // Compliant
60+
}
61+
62+
void longIsCompliant() {
63+
long w = 0L;
64+
w++; // Compliant
65+
++w; // Compliant
66+
w--; // Compliant
67+
--w; // Compliant
68+
}
69+
70+
void charIsCompliant() {
71+
char c = 'a';
72+
c++; // Compliant
73+
++c; // Compliant
74+
c--; // Compliant
75+
--c; // Compliant
76+
}
77+
78+
void otherOperatorsNotAffected() {
79+
float f = 1f;
80+
// test unary operator -
81+
float g = -f; // compliant
82+
// test binary operators just in case
83+
float h = f + 1f; // compliant
84+
}
85+
86+
// variable shadowing test
87+
private double d = 3.4;
88+
89+
void variableShadowingTestCompliant() {
90+
for (int d = 0; d < 10; d++) { // Compliant
91+
}
92+
}
93+
// variable shadowing test
94+
private int i = 3;
95+
96+
void variableShadowingTestCompliant() {
97+
for (float i = 0; i < 10; i++) { // Noncompliant {{Increment operator (++) should not be used with floating point variables}}
98+
}
99+
}
100+
}

java-checks/src/test/java/org/sonar/java/checks/IncDecOnFPCheckTest.java renamed to java-checks/src/test/java/org/sonar/java/checks/IncDecOnFloatingPointCheckTest.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,21 @@
1919
import org.junit.jupiter.api.Test;
2020
import org.sonar.java.checks.verifier.CheckVerifier;
2121

22-
class IncDecOnFPCheckTest {
22+
class IncDecOnFloatingPointCheckTest {
2323

2424
@Test
25-
void detected() {
25+
void test() {
2626
CheckVerifier.newVerifier()
27-
.onFile("src/test/files/checks/IncDecOnFPCheck.java")
28-
.withCheck(new IncDecOnFPCheck())
27+
.onFile("src/test/files/checks/IncDecOnFloatingPointCheck.java")
28+
.withCheck(new IncDecOnFloatingPointCheck())
29+
.verifyIssues();
30+
}
31+
@Test
32+
void testNoSemantic() {
33+
CheckVerifier.newVerifier()
34+
.withoutSemantic()
35+
.onFile("src/test/files/checks/IncDecOnFloatingPointCheck.java")
36+
.withCheck(new IncDecOnFloatingPointCheck())
2937
.verifyIssues();
3038
}
3139
}

0 commit comments

Comments
 (0)