Skip to content

Commit 318205b

Browse files
SONARJAVA-5522 Let DefaultInitializedFieldCheck handle underscores in...
...float/double literals
1 parent 4564d96 commit 318205b

3 files changed

Lines changed: 29 additions & 2 deletions

File tree

java-checks-test-sources/default/src/main/java/checks/DefaultInitializedFieldCheckSample.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,26 @@ class DefaultInitializedFieldCheckSample {
2121
float f1 = 0.f; // Noncompliant {{Remove this initialization to "0.f", the compiler will do that for you.}}
2222
float f2 = 1.f;
2323
float f3;
24+
float f4 = 1_000_000F; // Compliant, not 0
25+
float f5 = 1_000_000_000_000_000_000f; // Compliant, not 0
26+
float f6 = 1_000_000; // Compliant, not 0
27+
float f7 = 0_000_000; // Noncompliant {{Remove this initialization to "0_000_000", the compiler will do that for you.}}
28+
float f8 = 0_000_000f; // Noncompliant {{Remove this initialization to "0_000_000f", the compiler will do that for you.}}
29+
float f9 = (float) 1_000_000d; // Compliant, not 0
30+
float f10 = 123_456e-7f; // Compliant, not 0
31+
float f11 = 1_000.0f; // Compliant, not 0
2432
double d = 0.; // Noncompliant {{Remove this initialization to "0.", the compiler will do that for you.}}
2533
double d1 = 1.;
2634
double d2;
2735
double d3 = 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001; // Compliant, not 0
36+
double d4 = 1_000_000D; // Compliant, not 0
37+
double d5 = 1_000_000_000_000_000_000d; // Compliant, not 0
38+
double d6 = 1_000_000; // Compliant, not 0
39+
double d7 = 0_000_000; // Noncompliant {{Remove this initialization to "0_000_000", the compiler will do that for you.}}
40+
double d8 = 0_000_000d; // Noncompliant {{Remove this initialization to "0_000_000d", the compiler will do that for you.}}
41+
double d9 = 1_000_000f; // Compliant, not 0
42+
double d10 = 123_456e-7d; // Compliant, not 0
43+
double d11 = 1_000.0f; // Compliant, not 0
2844
char c = 0; // Noncompliant {{Remove this initialization to "0", the compiler will do that for you.}}
2945
char c1 = '\u0000'; // Noncompliant {{Remove this initialization to "'\u0000'", the compiler will do that for you.}}
3046
// ^^^^^^^^

java-checks/src/main/java/org/sonar/java/checks/DefaultInitializedFieldCheck.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,9 @@ private static Optional<String> getIfDefault(ExpressionTree expression, boolean
7979
.flatMap(numericalValue -> literalValue(expression));
8080
case FLOAT_LITERAL,
8181
DOUBLE_LITERAL:
82-
return literalValue(expression)
83-
.filter(numericalValue -> Double.doubleToLongBits(Double.valueOf(numericalValue)) == 0);
82+
return Optional.ofNullable(LiteralUtils.doubleLiteralValue(expression))
83+
.filter(numericalValue -> Double.doubleToLongBits(numericalValue) == 0)
84+
.flatMap(numericalValue -> literalValue(expression));
8485
case TYPE_CAST:
8586
return getIfDefault(((TypeCastTree) expression).expression(), isPrimitive);
8687
default:

java-frontend/src/main/java/org/sonar/java/model/LiteralUtils.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,16 @@ public static Long longLiteralValue(ExpressionTree tree) {
8585
return null;
8686
}
8787

88+
@CheckForNull
89+
public static Double doubleLiteralValue(ExpressionTree expression) {
90+
if (expression.is(Kind.FLOAT_LITERAL, Kind.DOUBLE_LITERAL)) {
91+
String value = ((LiteralTree) expression).value().replace("_", "");
92+
93+
return Double.parseDouble(value);
94+
}
95+
return null;
96+
}
97+
8898
@CheckForNull
8999
private static Integer minus(@Nullable Integer nullableInteger) {
90100
return nullableInteger == null ? null : -nullableInteger;

0 commit comments

Comments
 (0)