Skip to content

Commit 3d1c3d2

Browse files
authored
Merge branch 'main' into GCI104-python
2 parents 828e406 + 77f1ff0 commit 3d1c3d2

File tree

8 files changed

+241
-1
lines changed

8 files changed

+241
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12-
- [#77](https://github.com/green-code-initiative/creedengo-python/pull/77) Add rule GCI 104 AvoidCreatingTensorUsingNumpyOrNativePython, a rule specific to AI/ML code
12+
- [#77](https://github.com/green-code-initiative/creedengo-python/pull/77) Add rule GCI104 AvoidCreatingTensorUsingNumpyOrNativePython, a rule specific to AI/ML code
13+
- [#70](https://github.com/green-code-initiative/creedengo-python/pull/70) Add rule GCI108 Prefer Append Left (a rule to prefer the use of `append` over `insert` for list, using deques)
1314
- [#78](https://github.com/green-code-initiative/creedengo-python/pull/78) Add rule GCI105 on String Concatenation. This rule may also apply to other rules
1415
- [#74](https://github.com/green-code-initiative/creedengo-python/pull/74) Add rule GCI101 Avoid Conv Bias Before Batch Normalization, a rule specific to Deeplearning
1516
- [#75](https://github.com/green-code-initiative/creedengo-python/pull/75) Add rule GCI102 avoid non pinned memory for dataloader. This rule is specific to PyTorch and so AI

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
@@ -430,4 +430,19 @@ void testGCI107(){
430430
checkIssuesForFile(filePath, ruleId, ruleMsg, startLines, endLines, SEVERITY, TYPE, EFFORT_10MIN);
431431
}
432432

433+
@Test
434+
void testGCI108(){
435+
String filePath = "src/preferAppendLeft.py";
436+
String ruleId = "creedengo-python:GCI108";
437+
String ruleMsg = "Use appendleft with deque instead of .insert(0, val) for modification at the beginning of a list";
438+
int[] startLines = new int[]{
439+
5, 8, 11, 14, 17, 20, 23, 25, 31, 35, 42
440+
};
441+
int[] endLines = new int[]{
442+
5, 8, 11, 14, 17, 20, 23, 25, 31, 35, 42
443+
};
444+
445+
checkIssuesForFile(filePath, ruleId, ruleMsg, startLines, endLines, SEVERITY, TYPE, EFFORT_10MIN);
446+
}
447+
433448
}
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/PythonRuleRepository.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public class PythonRuleRepository implements RulesDefinition, PythonCustomRuleRe
5050
AvoidNonPinnedMemoryForDataloaders.class,
5151
AvoidConvBiasBeforeBatchNorm.class,
5252
StringConcatenation.class,
53+
PreferAppendLeft.class,
5354
AvoidCreatingTensorUsingNumpyOrNativePython.class
5455
);
5556

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* creedengo - Python language - Provides rules to reduce the environmental footprint of your Python programs
3+
* Copyright © 2024 Green Code Initiative (https://green-code-initiative.org)
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package org.greencodeinitiative.creedengo.python.checks;
19+
20+
import org.sonar.check.Rule;
21+
import org.sonar.plugins.python.api.PythonSubscriptionCheck;
22+
import org.sonar.plugins.python.api.SubscriptionContext;
23+
import org.sonar.plugins.python.api.tree.CallExpression;
24+
import org.sonar.plugins.python.api.tree.Expression;
25+
import org.sonar.plugins.python.api.tree.QualifiedExpression;
26+
import org.sonar.plugins.python.api.tree.RegularArgument;
27+
import org.sonar.plugins.python.api.tree.NumericLiteral;
28+
29+
import java.util.List;
30+
31+
import static org.sonar.plugins.python.api.tree.Tree.Kind.*;
32+
33+
@Rule(key = "GCI108")
34+
public class PreferAppendLeft extends PythonSubscriptionCheck {
35+
36+
public static final String DESCRIPTION = "Use appendleft with deque instead of .insert(0, val) for modification at the beginning of a list";
37+
38+
@Override
39+
public void initialize(Context context) {
40+
context.registerSyntaxNodeConsumer(CALL_EXPR, this::visitCallExpression);
41+
}
42+
43+
private void visitCallExpression(SubscriptionContext context) {
44+
CallExpression callExpression = (CallExpression) context.syntaxNode();
45+
if (callExpression.callee().is(QUALIFIED_EXPR)) {
46+
QualifiedExpression qualifiedExpression = (QualifiedExpression) callExpression.callee();
47+
if (qualifiedExpression.name().name().equals("insert")) {
48+
List<org.sonar.plugins.python.api.tree.Argument> arguments = callExpression.arguments();
49+
if (arguments.size() >= 2 && arguments.get(0) instanceof RegularArgument) {
50+
Expression firstArg = ((RegularArgument) arguments.get(0)).expression();
51+
if (firstArg.is(NUMERIC_LITERAL) && isZeroLiteral(firstArg)) {
52+
context.addIssue(callExpression, DESCRIPTION);
53+
}
54+
}
55+
}
56+
}
57+
}
58+
59+
private boolean isZeroLiteral(Expression expression) {
60+
if (expression.is(NUMERIC_LITERAL)) {
61+
NumericLiteral numericLiteral = (NumericLiteral) expression;
62+
String value = numericLiteral.valueAsString();
63+
try {
64+
return Double.parseDouble(value) == 0.0;
65+
} catch (NumberFormatException e) {
66+
return false;
67+
}
68+
}
69+
return false;
70+
}
71+
}

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
@@ -20,6 +20,7 @@
2020
"GCI105",
2121
"GCI106",
2222
"GCI107",
23+
"GCI108",
2324
"GCI203",
2425
"GCI404"
2526
]
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* creedengo - Python language - Provides rules to reduce the environmental footprint of your Python programs
3+
* Copyright © 2024 Green Code Initiative (https://green-code-initiative.org)
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package org.greencodeinitiative.creedengo.python.checks;
19+
20+
import org.junit.Test;
21+
import org.sonar.python.checks.utils.PythonCheckVerifier;
22+
23+
public class PreferAppendLeftTest {
24+
25+
@Test
26+
public void test() {
27+
PythonCheckVerifier.verify("src/test/resources/checks/preferAppendLeft.py", new PreferAppendLeft());
28+
}
29+
}
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+

0 commit comments

Comments
 (0)