Skip to content

Commit 4ef5761

Browse files
authored
SONARPY-2081: Array index in hex form should be handled correctly (#2055)
* SONARPY-2081 parse hex literals correctly * SONARPY-2081 add test case
1 parent 0e3ec87 commit 4ef5761

File tree

3 files changed

+6
-0
lines changed

3 files changed

+6
-0
lines changed

python-checks/src/test/resources/checks/useStartsWithEndsWithCheck.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def f():
5858
foobar[10:] == 'bar' # Noncompliant
5959

6060
foobar[3:6:1] == 'bar' # Compliant: Too much potential for FPs with step sizes != 1, too early stop indices, etc.
61+
foobar[3:6:0x1] == 'bar' # Same
6162
foobar[-3:] == 'bar' # Noncompliant {{Use `endswith` here.}}
6263
foobar[:-3] == 'foo' # Noncompliant {{Use `startswith` here.}}
6364

@@ -94,3 +95,4 @@ def __index__(self):
9495

9596
foobar[:3:2] == 'fo' # Compliant: This is not a simple step 1 slice, not a prefix, or suffix, so the rule does not apply
9697
foobar[:3:-2] == 'r' # Same
98+

python-frontend/src/main/java/org/sonar/python/tree/NumericLiteralImpl.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ public long valueAsLong() {
6262
}
6363
if (literalValue.startsWith("0o") || literalValue.startsWith("0O")) {
6464
return Integer.valueOf(literalValue.substring(2), 8);
65+
} else if (literalValue.startsWith("0x") || literalValue.startsWith("0X")) {
66+
return Integer.valueOf(literalValue.substring(2), 16);
6567
} else if (literalValue.startsWith("0") && literalValue.length() > 1) {
6668
// Python 2 syntax (https://www.python.org/dev/peps/pep-3127/#removal-of-old-octal-syntax)
6769
return Integer.valueOf(literalValue.substring(1), 8);

python-frontend/src/test/java/org/sonar/python/tree/PythonTreeMakerTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2019,6 +2019,8 @@ void numeric_literal_expression() {
20192019
testNumericLiteral("0o777", 511L);
20202020
testNumericLiteral("0O777", 511L);
20212021
testNumericLiteral("0777", 511L);
2022+
testNumericLiteral("0x28", 0x28L);
2023+
testNumericLiteral("0X28", 0x28L);
20222024
}
20232025

20242026
private void testNumericLiteral(String code, Long expectedValue) {

0 commit comments

Comments
 (0)