Skip to content

Commit 39996e7

Browse files
authored
Merge pull request #6 from green-code-initiative/EC34-replace
EC34 replaced by EC35 : python implementation
2 parents 14e5e7d + 753df7b commit 39996e7

File tree

7 files changed

+85
-52
lines changed

7 files changed

+85
-52
lines changed

CHANGELOG.md

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

1414
- [#5](https://github.com/green-code-initiative/ecoCode-python/pull/5) Upgrade licence system and licence headers of Java files
15+
- [#6](https://github.com/green-code-initiative/ecoCode-python/pull/6) Adding EC35 rule : EC35 rule replaces EC34 with a specific use case ("file not found" sepcific)
1516

1617
### Deleted
1718

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
<mockito.version>5.3.1</mockito.version>
6060

6161
<!-- temporary version waiting for real automatic release in ecocode repository -->
62-
<ecocode-rules-specifications.version>0.0.3</ecocode-rules-specifications.version>
62+
<ecocode-rules-specifications.version>0.0.6</ecocode-rules-specifications.version>
6363

6464
<sonar-analyzer-commons.version>2.5.0.1358</sonar-analyzer-commons.version>
6565

src/main/java/fr/greencodeinitiative/python/PythonRuleRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public List<Class> checkClasses() {
5959
AvoidGettersAndSetters.class,
6060
AvoidGlobalVariableInFunctionCheck.class,
6161
AvoidSQLRequestInLoop.class,
62-
AvoidTryCatchFinallyCheck.class,
62+
AvoidTryCatchWithFileOpenedCheck.class,
6363
AvoidUnoptimizedVectorImagesCheck.class,
6464
NoFunctionCallWhenDeclaringForLoop.class,
6565
AvoidFullSQLRequest.class,

src/main/java/fr/greencodeinitiative/python/checks/AvoidTryCatchFinallyCheck.java

Lines changed: 0 additions & 44 deletions
This file was deleted.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* ecoCode - Python language - Provides rules to reduce the environmental footprint of your Python programs
3+
* Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
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 fr.greencodeinitiative.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.symbols.Symbol;
24+
import org.sonar.plugins.python.api.tree.*;
25+
import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey;
26+
27+
import static org.sonar.plugins.python.api.tree.Tree.Kind.CALL_EXPR;
28+
29+
@Rule(key = "EC35")
30+
@DeprecatedRuleKey(repositoryKey = "gci-python", ruleKey = "S34")
31+
@DeprecatedRuleKey(repositoryKey = "gci-python", ruleKey = "EC34")
32+
public class AvoidTryCatchWithFileOpenedCheck extends PythonSubscriptionCheck {
33+
34+
public static final String DESCRIPTION = "Avoid the use of try-catch with a file open in try block";
35+
36+
@Override
37+
public void initialize(Context context) {
38+
context.registerSyntaxNodeConsumer(Tree.Kind.TRY_STMT, this::visitNode);
39+
}
40+
41+
private void visitNode(SubscriptionContext context) {
42+
TryStatement tryStatement = (TryStatement) context.syntaxNode();
43+
44+
for (Statement stmt : tryStatement.body().statements()){
45+
if (stmt.is(CALL_EXPR)) {
46+
CallExpression callExpression = (CallExpression) stmt;
47+
visitCallExpression(context, callExpression);
48+
} else {
49+
checkCallExpressionInChildren(context, stmt);
50+
}
51+
}
52+
53+
}
54+
55+
private void checkCallExpressionInChildren(SubscriptionContext context, Tree stmt) {
56+
stmt.children().forEach(tree -> {
57+
if (tree.is(CALL_EXPR)) {
58+
CallExpression callExpression = (CallExpression) tree;
59+
visitCallExpression(context, callExpression);
60+
} else {
61+
checkCallExpressionInChildren(context, tree);
62+
}
63+
});
64+
}
65+
66+
private void visitCallExpression(SubscriptionContext context, CallExpression callExpression){
67+
if ("open".equals(getFunctionNameFromCallExpression(callExpression))) {
68+
context.addIssue(callExpression.firstToken(), DESCRIPTION);
69+
}
70+
}
71+
72+
private String getFunctionNameFromCallExpression(CallExpression callExpression) {
73+
Symbol symbol = callExpression.calleeSymbol();
74+
return symbol != null && symbol.name() != null ? symbol.name() : "";
75+
}
76+
77+
}

src/test/java/fr/greencodeinitiative/python/checks/AvoidTryCatchFinallyCheckTest.java renamed to src/test/java/fr/greencodeinitiative/python/checks/AvoidTryCatchWithFileOpenedCheckTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
import org.junit.Test;
2222
import org.sonar.python.checks.utils.PythonCheckVerifier;
2323

24-
public class AvoidTryCatchFinallyCheckTest {
24+
public class AvoidTryCatchWithFileOpenedCheckTest {
2525
@Test
2626
public void test() {
27-
PythonCheckVerifier.verify("src/test/resources/checks/avoidTryCatchFinallyCheck.py", new AvoidTryCatchFinallyCheck());
27+
PythonCheckVerifier.verify("src/test/resources/checks/avoidTryCatchWithFileOpenedCheck.py", new AvoidTryCatchWithFileOpenedCheck());
2828
}
2929
}

src/test/resources/checks/avoidTryCatchFinallyCheck.py renamed to src/test/resources/checks/avoidTryCatchWithFileOpenedCheck.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,19 @@
22

33
path = 'hello.txt'
44

5-
65
def my_function():
76
x=0
87

9-
try: # Noncompliant {{Avoid the use of try-catch}}
8+
try:
109
print(x)
1110
except:
1211
print("Something went wrong")
1312
finally:
1413
print("The 'try except' is finished")
1514

1615
def foo():
17-
try: # Noncompliant {{Avoid the use of try-catch}}
18-
f = open(path)
16+
try:
17+
f = open(path) # Noncompliant {{Avoid the use of try-catch with a file open in try block}}
1918
print(f.read())
2019
except:
2120
print('No such file '+path)

0 commit comments

Comments
 (0)