Skip to content

Commit 2495510

Browse files
GCI108 PreferAppendLeft fix: change rule number (97->108), apply copilot suggestion, add IT
Co-authored-by: DataLabGroupe-CreditAgricole <[email protected]>
1 parent 4450e07 commit 2495510

File tree

4 files changed

+90
-6
lines changed

4 files changed

+90
-6
lines changed

src/it/java/org/greencodeinitiative/creedengo/python/integration/tests/GCIRulesIT.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,4 +414,19 @@ void testGCI107(){
414414
checkIssuesForFile(filePath, ruleId, ruleMsg, startLines, endLines, SEVERITY, TYPE, EFFORT_10MIN);
415415
}
416416

417+
@Test
418+
void testGCI108(){
419+
String filePath = "src/preferAppendLeft.py";
420+
String ruleId = "creedengo-python:GCI108";
421+
String ruleMsg = "Use appendleft with deque instead of .insert(0, val) for modification at the beginning of a list";
422+
int[] startLines = new int[]{
423+
5, 8, 11, 14, 17, 20, 23, 25, 31, 35, 42
424+
};
425+
int[] endLines = new int[]{
426+
5, 8, 11, 14, 17, 20, 23, 25, 31, 35, 42
427+
};
428+
429+
checkIssuesForFile(filePath, ruleId, ruleMsg, startLines, endLines, SEVERITY, TYPE, EFFORT_10MIN);
430+
}
431+
417432
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from collections import deque
2+
3+
# Cas non conformes
4+
numbers = []
5+
numbers.insert(0, val) # Noncompliant {{Use appendleft with deque instead of .insert(0, val) for modification at the beginning of a list}}
6+
7+
items = []
8+
items.insert(0, "start") # Noncompliant {{Use appendleft with deque instead of .insert(0, val) for modification at the beginning of a list}}
9+
10+
lst = []
11+
(lst).insert(0, 'x') # Noncompliant {{Use appendleft with deque instead of .insert(0, val) for modification at the beginning of a list}}
12+
13+
x = []
14+
(x).insert(0, value) # Noncompliant {{Use appendleft with deque instead of .insert(0, val) for modification at the beginning of a list}}
15+
16+
def insert_first(l, v):
17+
l.insert(0, v) # Noncompliant {{Use appendleft with deque instead of .insert(0, val) for modification at the beginning of a list}}
18+
19+
some_list = []
20+
some_list.insert(index=0, object=val) # Noncompliant {{Use appendleft with deque instead of .insert(0, val) for modification at the beginning of a list}}
21+
22+
deque_like = []
23+
deque_like.insert(0, "bad") # Noncompliant {{Use appendleft with deque instead of .insert(0, val) for modification at the beginning of a list}}
24+
25+
[1, 2, 3].insert(0, 9) # Noncompliant {{Use appendleft with deque instead of .insert(0, val) for modification at the beginning of a list}}
26+
27+
class MyList(list):
28+
pass
29+
30+
custom_list = MyList()
31+
custom_list.insert(0, 'z') # Noncompliant {{Use appendleft with deque instead of .insert(0, val) for modification at the beginning of a list}}
32+
33+
def wrapper():
34+
lst = []
35+
lst.insert(0, "wrapped") # Noncompliant {{Use appendleft with deque instead of .insert(0, val) for modification at the beginning of a list}}
36+
37+
38+
dq = deque()
39+
dq.appendleft("start")
40+
41+
mylist = []
42+
mylist.insert(0.0, val) # Noncompliant {{Use appendleft with deque instead of .insert(0, val) for modification at the beginning of a list}}
43+
44+
45+
data = []
46+
data.insert(1, "something")
47+
48+
real_deque = deque()
49+
real_deque.appendleft("good")
50+
51+
52+
other_list = []
53+
position = 1
54+
other_list.insert(position, "ok")
55+
56+
57+
58+
val = "new"
59+
queue = deque()
60+
queue.appendleft(val)
61+

src/main/java/org/greencodeinitiative/creedengo/python/checks/PreferAppendLeft.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import static org.sonar.plugins.python.api.tree.Tree.Kind.*;
3232

3333

34-
@Rule(key = "GCI97")
34+
@Rule(key = "GCI108")
3535
public class PreferAppendLeft extends PythonSubscriptionCheck {
3636
public static final String DESCRIPTION = "Use appendleft with deque instead of .insert(0, val) for modification at the beginning of a list";
3737

@@ -50,10 +50,13 @@ private void visitCallExpression(SubscriptionContext context) {
5050
if (arguments.size() >= 2) {
5151
Expression firstArg;
5252
firstArg = ((RegularArgument) arguments.get(0)).expression();
53-
if (firstArg.is(NUMERIC_LITERAL)) {
54-
if (isZeroLiteral(firstArg)) {
55-
context.addIssue(callExpression, DESCRIPTION);
56-
}
53+
if (arguments.get(0) instanceof RegularArgument) {
54+
firstArg = ((RegularArgument) arguments.get(0)).expression();
55+
if (firstArg.is(NUMERIC_LITERAL)) {
56+
if (isZeroLiteral(firstArg)) {
57+
context.addIssue(callExpression, DESCRIPTION);
58+
}
59+
}
5760
}
5861
}
5962
}
@@ -64,7 +67,11 @@ private boolean isZeroLiteral(Expression expression) {
6467
if (expression.is(NUMERIC_LITERAL)) {
6568
NumericLiteral numericLiteral = (NumericLiteral) expression;
6669
String value = numericLiteral.valueAsString();
67-
return "0".equals(value) || "0.0".equals(value) || "0.00".equals(value);
70+
try {
71+
return Double.parseDouble(value) == 0.0;
72+
} catch (NumberFormatException e) {
73+
return false;
74+
}
6875
}
6976
return false;
7077
}

src/main/resources/org/greencodeinitiative/creedengo/python/creedengo_way_profile.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"GCI105",
2020
"GCI106",
2121
"GCI107",
22+
"GCI108",
2223
"GCI203",
2324
"GCI404"
2425
]

0 commit comments

Comments
 (0)