Skip to content

Commit e557fc5

Browse files
GCI97 Add unit tests and detect 0.0 and 0.00 case
Co-authored-by: DataLabGroupe-CreditAgricole <[email protected]>
1 parent e387e18 commit e557fc5

File tree

2 files changed

+42
-28
lines changed

2 files changed

+42
-28
lines changed

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
import static org.sonar.plugins.python.api.tree.Tree.Kind.*;
3232

33+
3334
@Rule(key = "GCI97")
3435
public class PreferAppendLeft extends PythonSubscriptionCheck {
3536
public static final String DESCRIPTION = "Use appendleft with deque instead of .insert(0, val) for modification at the beginning of a list";
@@ -41,21 +42,18 @@ public void initialize(Context context) {
4142

4243
private void visitCallExpression(SubscriptionContext context) {
4344
CallExpression callExpression = (CallExpression) context.syntaxNode();
44-
4545
if (callExpression.callee().is(QUALIFIED_EXPR)) {
4646
QualifiedExpression qualifiedExpression = (QualifiedExpression) callExpression.callee();
47-
48-
4947

5048
if (qualifiedExpression.name().name().equals("insert")) {
5149
List<org.sonar.plugins.python.api.tree.Argument> arguments = callExpression.arguments();
52-
53-
if (arguments.size() >= 2) {// because it should be like insert(0,val) so there's two arguments
50+
if (arguments.size() >= 2) {
5451
Expression firstArg;
5552
firstArg = ((RegularArgument) arguments.get(0)).expression();
56-
57-
if (isZeroLiteral(firstArg)) {
53+
if (firstArg.is(NUMERIC_LITERAL)) {
54+
if (isZeroLiteral(firstArg)) {
5855
context.addIssue(callExpression, DESCRIPTION);
56+
}
5957
}
6058
}
6159
}
@@ -66,7 +64,7 @@ private boolean isZeroLiteral(Expression expression) {
6664
if (expression.is(NUMERIC_LITERAL)) {
6765
NumericLiteral numericLiteral = (NumericLiteral) expression;
6866
String value = numericLiteral.valueAsString();
69-
return "0".equals(value);
67+
return "0".equals(value) || "0.0".equals(value) || "0.00".equals(value);
7068
}
7169
return false;
7270
}
Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,12 @@
11
from collections import deque
22

3+
# Cas non conformes
34
numbers = []
45
numbers.insert(0, val) # Noncompliant {{Use appendleft with deque instead of .insert(0, val) for modification at the beginning of a list}}
56

67
items = []
78
items.insert(0, "start") # Noncompliant {{Use appendleft with deque instead of .insert(0, val) for modification at the beginning of a list}}
89

9-
mylist = []
10-
mylist.insert(0.0, val)
11-
12-
a = 0
13-
mylist.insert(a, val)
14-
15-
dq = deque()
16-
dq.appendleft("start")
17-
18-
data = []
19-
data.insert(1, "something")
20-
2110
lst = []
2211
(lst).insert(0, 'x') # Noncompliant {{Use appendleft with deque instead of .insert(0, val) for modification at the beginning of a list}}
2312

@@ -27,19 +16,46 @@
2716
def insert_first(l, v):
2817
l.insert(0, v) # Noncompliant {{Use appendleft with deque instead of .insert(0, val) for modification at the beginning of a list}}
2918

30-
def add_to_front(dq, item):
31-
dq.appendleft(item)
32-
3319
some_list = []
34-
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}}
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}}
3521

3622
deque_like = []
3723
deque_like.insert(0, "bad") # Noncompliant {{Use appendleft with deque instead of .insert(0, val) for modification at the beginning of a list}}
3824

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+
3948
real_deque = deque()
40-
real_deque.appendleft("good")
49+
real_deque.appendleft("good")
4150

42-
fn = getattr(mylist, "insert")
43-
fn(0, val)
4451

45-
[1, 2, 3].insert(0, 9) # Noncompliant {{Use appendleft with deque instead of .insert(0, val) for modification at the beginning of a list}}
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+

0 commit comments

Comments
 (0)