Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EC34 replaced by EC35 : python implementation #6

Merged
merged 6 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,81 @@
/*
* 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){
switch (getFunctionNameFromCallExpression(callExpression)) {
case "open":
context.addIssue(callExpression.firstToken(), DESCRIPTION);
break;
default:
break;
}
}

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