Merge branch 'pr/5020'

Ref #5020
This commit is contained in:
Clément Fournier
2024-05-21 15:55:50 +02:00
5 changed files with 80 additions and 6 deletions

View File

@ -44,6 +44,7 @@ Since this release, PMD will also expose any getter returning a collection of an
* [#4930](https://github.com/pmd/pmd/issues/4930): \[java] EmptyControlStatement should not allow empty try with concise resources
* java-errorprone
* [#4042](https://github.com/pmd/pmd/issues/4042): \[java] A false negative about the rule StringBufferInstantiationWithChar
* [#5007](https://github.com/pmd/pmd/issues/5007): \[java] AvoidUsingOctalValues triggers on non-octal double literals with a leading 0
* java-multithreading
* [#2368](https://github.com/pmd/pmd/issues/2368): \[java] False positive UnsynchronizedStaticFormatter in static initializer
@ -56,5 +57,7 @@ Since this release, PMD will also expose any getter returning a collection of an
### ✨ External Contributions
* [#5020](https://github.com/pmd/pmd/issues/5020): \[java] Fix AvoidUsingOctalValues false-positive - [Gold856](https://github.com/Gold856) (@Gold856)
{% endtocmaker %}

View File

@ -120,10 +120,10 @@ public final class ASTNumericLiteral extends AbstractLiteral {
* for the literal {@code 0} (which can really be any base).
*/
public int getBase() {
return getBase(getLiteralText());
return getBase(getLiteralText(), isIntegral());
}
static int getBase(Chars image) {
static int getBase(Chars image, boolean isIntegral) {
if (image.length() > 1 && image.charAt(0) == '0') {
switch (image.charAt(1)) {
case 'x':
@ -132,10 +132,8 @@ public final class ASTNumericLiteral extends AbstractLiteral {
case 'b':
case 'B':
return 2;
case '.':
return 10;
default:
return 8;
return isIntegral ? 8 : 10;
}
}
return 10;
@ -172,7 +170,7 @@ public final class ASTNumericLiteral extends AbstractLiteral {
* <p>Invalid literals or overflows result in {@code 0L}.
*/
static long parseIntegralValue(Chars image) {
final int base = getBase(image);
final int base = getBase(image, true);
if (base == 8) {
image = image.subSequence(1); // 0
} else if (base != 10) {

View File

@ -408,6 +408,23 @@ $delim
it::getBase shouldBe 10
}
}
"0." should parseAs {
number(DOUBLE) {
it::getBase shouldBe 10
}
}
val doubleOrFloatInBase10: NodeSpec<*> = {
number {
it::getBase shouldBe 10
}
}
"05e10" should parseAs(doubleOrFloatInBase10)
"05e10f" should parseAs(doubleOrFloatInBase10)
"00f" should parseAs(doubleOrFloatInBase10)
"00d" should parseAs(doubleOrFloatInBase10)
"00D" should parseAs(doubleOrFloatInBase10)
"050.0" should parseAs(doubleOrFloatInBase10)
}
}
@ -424,6 +441,7 @@ $delim
it::getValueAsDouble shouldBe 30.0
it::getValueAsFloat shouldBe 30f
it::getValueAsInt shouldBe 30
it::getBase shouldBe 16
}
}
@ -436,6 +454,7 @@ $delim
it::getValueAsDouble shouldBe 7.5
it::getValueAsFloat shouldBe 7.5f
it::getValueAsInt shouldBe 7
it::getBase shouldBe 16
}
}
@ -458,6 +477,7 @@ $delim
it::getValueAsFloat shouldBe 15f
it::getValueAsInt shouldBe 15
it::getValueAsLong shouldBe 15L
it::getBase shouldBe 16
}
}
@ -508,6 +528,7 @@ $delim
it::getValueAsFloat shouldBe 3f
it::getValueAsInt shouldBe 3
it::getValueAsLong shouldBe 3L
it::getBase shouldBe 2
}
}

View File

@ -244,6 +244,16 @@ public class Foo {
]]></code>
</test-code>
<test-code>
<description>ok, float literal in hexadecimal and exponent</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
float f = 0x1.0000_ffff_eeeep+2f;
}
]]></code>
</test-code>
<test-code>
<description>ok, Lengthy numeric literal with variable name as serialVersionUID</description>
<expected-problems>0</expected-problems>

View File

@ -54,6 +54,48 @@ public class Foo {
]]></code>
</test-code>
<test-code>
<description>OK, double value</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
double d = 012.0;
}
]]></code>
</test-code>
<test-code>
<description>OK, double suffix</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
double d1 = 012d;
double d2 = 012D;
}
]]></code>
</test-code>
<test-code>
<description>OK, float suffix</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
float f = 012f;
float f = 012F;
}
]]></code>
</test-code>
<test-code>
<description>OK, double value with exponent</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
double d = 012e0;
}
]]></code>
</test-code>
<test-code>
<description>bad, 012L</description>
<expected-problems>1</expected-problems>