Skip to content

Commit

Permalink
Merge pull request #6 from green-code-initiative/EC34-replace
Browse files Browse the repository at this point in the history
EC34 replaced by EC35 : python implementation
  • Loading branch information
dedece35 authored Oct 29, 2023
2 parents 14e5e7d + 753df7b commit 39996e7
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 52 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

- [#5](https://github.com/green-code-initiative/ecoCode-python/pull/5) Upgrade licence system and licence headers of Java files
- [#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)

### Deleted

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
<mockito.version>5.3.1</mockito.version>

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public List<Class> checkClasses() {
AvoidGettersAndSetters.class,
AvoidGlobalVariableInFunctionCheck.class,
AvoidSQLRequestInLoop.class,
AvoidTryCatchFinallyCheck.class,
AvoidTryCatchWithFileOpenedCheck.class,
AvoidUnoptimizedVectorImagesCheck.class,
NoFunctionCallWhenDeclaringForLoop.class,
AvoidFullSQLRequest.class,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* ecoCode - Python language - Provides rules to reduce the environmental footprint of your Python programs
* Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package fr.greencodeinitiative.python.checks;

import org.sonar.check.Rule;
import org.sonar.plugins.python.api.PythonSubscriptionCheck;
import org.sonar.plugins.python.api.SubscriptionContext;
import org.sonar.plugins.python.api.symbols.Symbol;
import org.sonar.plugins.python.api.tree.*;
import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey;

import static org.sonar.plugins.python.api.tree.Tree.Kind.CALL_EXPR;

@Rule(key = "EC35")
@DeprecatedRuleKey(repositoryKey = "gci-python", ruleKey = "S34")
@DeprecatedRuleKey(repositoryKey = "gci-python", ruleKey = "EC34")
public class AvoidTryCatchWithFileOpenedCheck extends PythonSubscriptionCheck {

public static final String DESCRIPTION = "Avoid the use of try-catch with a file open in try block";

@Override
public void initialize(Context context) {
context.registerSyntaxNodeConsumer(Tree.Kind.TRY_STMT, this::visitNode);
}

private void visitNode(SubscriptionContext context) {
TryStatement tryStatement = (TryStatement) context.syntaxNode();

for (Statement stmt : tryStatement.body().statements()){
if (stmt.is(CALL_EXPR)) {
CallExpression callExpression = (CallExpression) stmt;
visitCallExpression(context, callExpression);
} else {
checkCallExpressionInChildren(context, stmt);
}
}

}

private void checkCallExpressionInChildren(SubscriptionContext context, Tree stmt) {
stmt.children().forEach(tree -> {
if (tree.is(CALL_EXPR)) {
CallExpression callExpression = (CallExpression) tree;
visitCallExpression(context, callExpression);
} else {
checkCallExpressionInChildren(context, tree);
}
});
}

private void visitCallExpression(SubscriptionContext context, CallExpression callExpression){
if ("open".equals(getFunctionNameFromCallExpression(callExpression))) {
context.addIssue(callExpression.firstToken(), DESCRIPTION);
}
}

private String getFunctionNameFromCallExpression(CallExpression callExpression) {
Symbol symbol = callExpression.calleeSymbol();
return symbol != null && symbol.name() != null ? symbol.name() : "";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
import org.junit.Test;
import org.sonar.python.checks.utils.PythonCheckVerifier;

public class AvoidTryCatchFinallyCheckTest {
public class AvoidTryCatchWithFileOpenedCheckTest {
@Test
public void test() {
PythonCheckVerifier.verify("src/test/resources/checks/avoidTryCatchFinallyCheck.py", new AvoidTryCatchFinallyCheck());
PythonCheckVerifier.verify("src/test/resources/checks/avoidTryCatchWithFileOpenedCheck.py", new AvoidTryCatchWithFileOpenedCheck());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@

path = 'hello.txt'


def my_function():
x=0

try: # Noncompliant {{Avoid the use of try-catch}}
try:
print(x)
except:
print("Something went wrong")
finally:
print("The 'try except' is finished")

def foo():
try: # Noncompliant {{Avoid the use of try-catch}}
f = open(path)
try:
f = open(path) # Noncompliant {{Avoid the use of try-catch with a file open in try block}}
print(f.read())
except:
print('No such file '+path)
Expand Down

0 comments on commit 39996e7

Please sign in to comment.