Skip to content

Commit a520fc0

Browse files
authored
Merge pull request #70 from cleophass/GCI97-python
GCI108 PreferAppendLeft #Python #DLG #Build
2 parents e9c432b + 2495510 commit a520fc0

File tree

8 files changed

+248
-1
lines changed

8 files changed

+248
-1
lines changed

CHANGELOG.md

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

1010
### Added
1111

12+
- [#70](https://github.com/green-code-initiative/creedengo-python/pull/70) Add rule GCI97 Prefer Append Left (a rule to prefer the use of `append` over `insert` for list, using deques)
1213
- [#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
1314
- [#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
1415
- [#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
@@ -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/PythonRuleRepository.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ public class PythonRuleRepository implements RulesDefinition, PythonCustomRuleRe
4949
AvoidIterativeMatrixOperations.class,
5050
AvoidNonPinnedMemoryForDataloaders.class,
5151
AvoidConvBiasBeforeBatchNorm.class,
52-
StringConcatenation.class
52+
StringConcatenation.class,
53+
PreferAppendLeft.class
5354
);
5455

5556
public static final String LANGUAGE = "py";
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
34+
@Rule(key = "GCI108")
35+
public class PreferAppendLeft extends PythonSubscriptionCheck {
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+
48+
if (qualifiedExpression.name().name().equals("insert")) {
49+
List<org.sonar.plugins.python.api.tree.Argument> arguments = callExpression.arguments();
50+
if (arguments.size() >= 2) {
51+
Expression firstArg;
52+
firstArg = ((RegularArgument) arguments.get(0)).expression();
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+
}
60+
}
61+
}
62+
}
63+
}
64+
}
65+
66+
private boolean isZeroLiteral(Expression expression) {
67+
if (expression.is(NUMERIC_LITERAL)) {
68+
NumericLiteral numericLiteral = (NumericLiteral) expression;
69+
String value = numericLiteral.valueAsString();
70+
try {
71+
return Double.parseDouble(value) == 0.0;
72+
} catch (NumberFormatException e) {
73+
return false;
74+
}
75+
}
76+
return false;
77+
}
78+
}

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
]
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)