Skip to content

Commit 2605edb

Browse files
authored
Merge branch 'main' into GCI95-python
2 parents bd8ecff + 238f78f commit 2605edb

File tree

11 files changed

+689
-4
lines changed

11 files changed

+689
-4
lines changed

CHANGELOG.md

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

1212
- [#69](https://github.com/green-code-initiative/creedengo-python/pull/69) Add rule GCI99 Avoid CSV Format
13+
- [#76](https://github.com/green-code-initiative/creedengo-python/pull/76) Add rule GCI103 Dictionary Items Unused. A rule specifying that dictionary iteration should consider the pertinence of the element used.
1314
- [#79](https://github.com/green-code-initiative/creedengo-python/pull/79) Add rule GCI106 Avoid SQRT in a loop
1415
- [#71](https://github.com/green-code-initiative/creedengo-python/pull/71) Add rule GCI96 Require Usecols Argument in Pandas Read Functions
1516
- [#72](https://github.com/green-code-initiative/creedengo-python/pull/72) Add rule GCI97 Optimize square computation (scalar vs vectorized method)

pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@
5050
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
5151

5252
<!-- Minimal version of the SonarQube APIs for `creedengo-python-plugin` -->
53-
<sonarqube-plugin-api-min.version>9.9.0.65466</sonarqube-plugin-api-min.version>
53+
<sonarqube-plugin-api-min.version>9.9.0.65466</sonarqube-plugin-api-min.version>
5454

5555
<!-- Version of the SonarQube APIs used by `creedengo-java-plugin` -->
56-
<sonar.plugin.api.version>11.4.0.2922</sonar.plugin.api.version>
56+
<sonar.plugin.api.version>11.4.0.2922</sonar.plugin.api.version>
5757

58-
<sonar-analyzer-commons.version>2.17.0.3322</sonar-analyzer-commons.version>
58+
<sonar-analyzer-commons.version>2.17.0.3322</sonar-analyzer-commons.version>
5959

6060
<!-- last version that all is OK -->
6161
<sonarpython.version>4.6.0.12071</sonarpython.version>
@@ -66,7 +66,7 @@
6666
<!-- <sonarpython.version>4.22.0.16914</sonarpython.version>-->
6767
<!-- <sonarpython.version>5.4.0.22255</sonarpython.version>-->
6868

69-
<mockito.version>5.17.0</mockito.version>
69+
<mockito.version>5.17.0</mockito.version>
7070
<lombok.version>1.18.38</lombok.version>
7171

7272
<!-- temporary version waiting for a real automatic release in creedengo repository -->

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,23 @@ void testGCI97(){
302302

303303
checkIssuesForFile(filePath, ruleId, ruleMsg, startLines, endLines, SEVERITY, TYPE, EFFORT_1MIN);
304304
}
305+
306+
@Test
307+
void testGCI103(){
308+
309+
String filePath = "src/dictionaryItemsUnused.py";
310+
String ruleId = "creedengo-python:GCI103";
311+
String ruleMsg = "Use dict.keys() or dict.values() instead of dict.items() when only one part of the key-value pair is used";
312+
int[] startLines = new int[]{
313+
5, 8, 24, 27, 36
314+
};
315+
int[] endLines = new int[]{
316+
5, 8, 24, 27, 36
317+
};
318+
319+
checkIssuesForFile(filePath, ruleId, ruleMsg, startLines, endLines, SEVERITY, TYPE, EFFORT_1MIN);
320+
321+
}
305322

306323
@Test
307324
void testGCI99(){
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
for a, b in my_dict.items():
3+
print(a, b)
4+
5+
for key, value in my_dict.items(): # Noncompliant {{Use dict.keys() or dict.values() instead of dict.items() when only one part of the key-value pair is used}}
6+
result.append(key)
7+
8+
for key, value in my_dict.items(): # Noncompliant {{Use dict.keys() or dict.values() instead of dict.items() when only one part of the key-value pair is used}}
9+
result.append(value)
10+
11+
for key in my_dict.keys():
12+
result.append(key)
13+
14+
for value in my_dict.values():
15+
result.append(value)
16+
17+
for item in my_dict.items():
18+
result.append(item)
19+
20+
entries = []
21+
for k, v in my_dict.items():
22+
entries.append((k, v))
23+
24+
for key, value in my_dict.items(): # Noncompliant {{Use dict.keys() or dict.values() instead of dict.items() when only one part of the key-value pair is used}}
25+
do_something_with(key)
26+
27+
for k, v in my_dict.items(): # Noncompliant {{Use dict.keys() or dict.values() instead of dict.items() when only one part of the key-value pair is used}}
28+
do_something_with(v)
29+
30+
for key, value in my_dict.items():
31+
print(f"{key}: {value}")
32+
33+
for k, v in my_dict.items():
34+
some_list.append((k, v))
35+
36+
for k, v in my_dict.items(): # Noncompliant {{Use dict.keys() or dict.values() instead of dict.items() when only one part of the key-value pair is used}}
37+
used_keys.append(k)
38+
39+
if True:
40+
for k, v in my_dict.items():
41+
print(k)
42+
print(v)
43+
44+
copied_dict = dict(my_dict.items())
45+
46+
for i, (k, v) in enumerate(my_dict.items()):
47+
print(i, k, v)
48+
49+
{(k, v) for k, v in my_dict.items()}

src/main/java/org/greencodeinitiative/creedengo/python/PythonRuleRepository.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public class PythonRuleRepository implements RulesDefinition, PythonCustomRuleRe
4444
PandasRequireUsecolsArgument.class,
4545
OptimizeSquareComputation.class,
4646
AvoidSqrtInLoop.class,
47+
DictionaryItemsUnused.class,
4748
AvoidCSVFormat.class
4849
);
4950

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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 java.util.HashMap;
21+
import java.util.Map;
22+
23+
import org.sonar.check.Rule;
24+
import org.sonar.plugins.python.api.PythonSubscriptionCheck;
25+
import org.sonar.plugins.python.api.SubscriptionContext;
26+
import org.sonar.plugins.python.api.tree.CallExpression;
27+
import org.sonar.plugins.python.api.tree.Expression;
28+
import org.sonar.plugins.python.api.tree.ForStatement;
29+
import org.sonar.plugins.python.api.tree.Name;
30+
import org.sonar.plugins.python.api.tree.QualifiedExpression;
31+
import org.sonar.plugins.python.api.tree.Tree;
32+
33+
34+
@Rule(key = "GCI103")
35+
public class DictionaryItemsUnused extends PythonSubscriptionCheck {
36+
37+
public static final String DESCRIPTION = "Use dict.keys() or dict.values() instead of dict.items() when only one part of the key-value pair is used";
38+
39+
private final Map<ForStatement, ItemsLoopInfo> itemsLoops = new HashMap<>();
40+
41+
@Override
42+
public void initialize(Context context) {
43+
context.registerSyntaxNodeConsumer(Tree.Kind.FOR_STMT, this::processForLoop);
44+
}
45+
46+
private void processForLoop(SubscriptionContext context) {
47+
ForStatement forStmt = (ForStatement) context.syntaxNode();
48+
49+
if (forStmt.expressions().size() == 2) {
50+
Expression keyExpr = forStmt.expressions().get(0);
51+
Expression valueExpr = forStmt.expressions().get(1);
52+
Expression iterable = forStmt.testExpressions().get(0);
53+
54+
if (isItemsCall(iterable)) {
55+
String key = keyExpr.is(Tree.Kind.NAME) ? ((Name) keyExpr).name() : null;
56+
String value = valueExpr.is(Tree.Kind.NAME) ? ((Name) valueExpr).name() : null;
57+
58+
if (key != null && value != null) {
59+
ItemsLoopInfo info = new ItemsLoopInfo(key, value);
60+
itemsLoops.put(forStmt, info);
61+
62+
trackNameUsages(forStmt.body(), info);
63+
}
64+
}
65+
}
66+
67+
finalizeCheck(context);
68+
}
69+
70+
private boolean isItemsCall(Expression expr) {
71+
if (expr.is(Tree.Kind.CALL_EXPR)) {
72+
CallExpression callExpr = (CallExpression) expr;
73+
if (callExpr.callee().is(Tree.Kind.QUALIFIED_EXPR)) {
74+
QualifiedExpression qualExpr = (QualifiedExpression) callExpr.callee();
75+
return "items".equals(qualExpr.name().name());
76+
}
77+
}
78+
return false;
79+
}
80+
81+
private void trackNameUsages(Tree node, ItemsLoopInfo info) {
82+
if (node instanceof Name nodeName) {
83+
info.markUsage(nodeName.name());
84+
}
85+
86+
for (Tree child : node.children()) {
87+
trackNameUsages(child, info);
88+
}
89+
}
90+
91+
private void finalizeCheck(SubscriptionContext context) {
92+
ForStatement forStmt = (ForStatement) context.syntaxNode();
93+
ItemsLoopInfo info = itemsLoops.get(forStmt);
94+
95+
if (info != null) {
96+
97+
if (info.isOnlyOneUsed()) {
98+
context.addIssue(forStmt.firstToken(), DESCRIPTION);
99+
}
100+
101+
itemsLoops.remove(forStmt);
102+
}
103+
}
104+
105+
private static class ItemsLoopInfo {
106+
final String keyVar;
107+
final String valueVar;
108+
boolean keyUsed = false;
109+
boolean valueUsed = false;
110+
111+
ItemsLoopInfo(String keyVar, String valueVar) {
112+
this.keyVar = keyVar;
113+
this.valueVar = valueVar;
114+
}
115+
116+
void markUsage(String val) {
117+
if (val.equals(keyVar)) {
118+
keyUsed = true;
119+
}
120+
if (val.equals(valueVar)) {
121+
valueUsed = true;
122+
}
123+
}
124+
125+
boolean isOnlyOneUsed() {
126+
return (keyUsed && !valueUsed) || (!keyUsed && valueUsed);
127+
}
128+
}
129+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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.utils;
19+
20+
import org.sonar.plugins.python.api.SubscriptionContext;
21+
import org.sonar.plugins.python.api.symbols.Symbol;
22+
import org.sonar.plugins.python.api.tree.*;
23+
24+
import javax.annotation.CheckForNull;
25+
import java.util.List;
26+
import java.util.Optional;
27+
28+
public class UtilsAST {
29+
30+
private UtilsAST() {
31+
// Utility class - prevent instantiation
32+
}
33+
34+
public static List<Argument> getArgumentsFromCall(CallExpression callExpression) {
35+
return Optional.ofNullable(callExpression)
36+
.map(CallExpression::argumentList)
37+
.map(ArgList::arguments)
38+
.orElse(List.of());
39+
}
40+
41+
public static String getMethodName(CallExpression callExpression) {
42+
return Optional.ofNullable(callExpression)
43+
.map(CallExpression::calleeSymbol)
44+
.map(Symbol::name)
45+
.orElse("");
46+
}
47+
48+
public static String getQualifiedName(CallExpression callExpression) {
49+
return Optional.ofNullable(callExpression)
50+
.map(CallExpression::calleeSymbol)
51+
.map(Symbol::fullyQualifiedName)
52+
.orElse("");
53+
}
54+
55+
public static String getVariableName(SubscriptionContext context) {
56+
57+
if (context == null || context.syntaxNode() == null) {
58+
return null;
59+
}
60+
61+
Tree current = context.syntaxNode();
62+
while (current != null && !current.is(Tree.Kind.ASSIGNMENT_STMT)) {
63+
current = current.parent();
64+
}
65+
if (current != null) {
66+
AssignmentStatement assignment = (AssignmentStatement) current;
67+
if (hasNonEmptyLhsExpressions(assignment)) {
68+
Expression leftExpr = assignment.lhsExpressions().get(0).expressions().get(0);
69+
if (leftExpr.is(Tree.Kind.NAME)) {
70+
Name variableName = (Name) leftExpr;
71+
return variableName.name();
72+
}
73+
}
74+
75+
}
76+
return null;
77+
}
78+
79+
@CheckForNull
80+
public static RegularArgument nthArgumentOrKeyword(int argPosition, String keyword, List<Argument> arguments) {
81+
return arguments.stream()
82+
.filter(argument -> hasKeyword(argument, keyword) ||
83+
(argument.is(Tree.Kind.REGULAR_ARGUMENT)
84+
&& ((RegularArgument) argument).keywordArgument() == null
85+
&& arguments.indexOf(argument) == argPosition))
86+
.map(RegularArgument.class::cast)
87+
.findFirst()
88+
.orElse(null);
89+
}
90+
91+
private static boolean hasKeyword(Argument argument, String keyword) {
92+
return argument instanceof RegularArgument regularArgument &&
93+
Optional.ofNullable(regularArgument.keywordArgument())
94+
.map(Name::name)
95+
.filter(name -> name.equals(keyword))
96+
.isPresent();
97+
}
98+
99+
private static boolean hasNonEmptyLhsExpressions(AssignmentStatement assignment ) {
100+
return Optional.ofNullable(assignment.lhsExpressions())
101+
.filter(lhs -> !lhs.isEmpty())
102+
.map(lhs -> lhs.get(0).expressions())
103+
.filter(exprLst -> !exprLst.isEmpty())
104+
.isPresent();
105+
}
106+
}

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
@@ -13,6 +13,7 @@
1313
"GCI96",
1414
"GCI97",
1515
"GCI99",
16+
"GCI103",
1617
"GCI106",
1718
"GCI203",
1819
"GCI404"
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 DictionaryItemsUnusedTest {
24+
25+
@Test
26+
public void test() {
27+
PythonCheckVerifier.verify("src/test/resources/checks/dictionaryItemsUnused.py", new DictionaryItemsUnused());
28+
}
29+
}

0 commit comments

Comments
 (0)