From 66c0ee697932eb58c07cf9427f772491a93a6bdb Mon Sep 17 00:00:00 2001 From: David DE CARVALHO Date: Sat, 21 Oct 2023 23:18:44 +0200 Subject: [PATCH 1/6] [PR 128-ecocode] replace EC34 by EC35 : update Python implem --- .../python/PythonRuleRepository.java | 2 +- .../checks/AvoidTryCatchFileOpenCheck.java | 85 +++++++++++++++++++ .../checks/AvoidTryCatchFinallyCheck.java | 44 ---------- .../checks/AvoidTryCatchFinallyCheckTest.java | 2 +- ...k.py => avoidTryCatchWithFileOpenCheck.py} | 6 +- 5 files changed, 90 insertions(+), 49 deletions(-) create mode 100644 src/main/java/fr/greencodeinitiative/python/checks/AvoidTryCatchFileOpenCheck.java delete mode 100644 src/main/java/fr/greencodeinitiative/python/checks/AvoidTryCatchFinallyCheck.java rename src/test/resources/checks/{avoidTryCatchFinallyCheck.py => avoidTryCatchWithFileOpenCheck.py} (75%) diff --git a/src/main/java/fr/greencodeinitiative/python/PythonRuleRepository.java b/src/main/java/fr/greencodeinitiative/python/PythonRuleRepository.java index 4374f3c..3202891 100644 --- a/src/main/java/fr/greencodeinitiative/python/PythonRuleRepository.java +++ b/src/main/java/fr/greencodeinitiative/python/PythonRuleRepository.java @@ -59,7 +59,7 @@ public List checkClasses() { AvoidGettersAndSetters.class, AvoidGlobalVariableInFunctionCheck.class, AvoidSQLRequestInLoop.class, - AvoidTryCatchFinallyCheck.class, + AvoidTryCatchFileOpenCheck.class, AvoidUnoptimizedVectorImagesCheck.class, NoFunctionCallWhenDeclaringForLoop.class, AvoidFullSQLRequest.class, diff --git a/src/main/java/fr/greencodeinitiative/python/checks/AvoidTryCatchFileOpenCheck.java b/src/main/java/fr/greencodeinitiative/python/checks/AvoidTryCatchFileOpenCheck.java new file mode 100644 index 0000000..36f31bd --- /dev/null +++ b/src/main/java/fr/greencodeinitiative/python/checks/AvoidTryCatchFileOpenCheck.java @@ -0,0 +1,85 @@ +/* + * 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 . + */ +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 java.util.List; +import java.util.Objects; + +import static org.sonar.plugins.python.api.tree.Tree.Kind.CALL_EXPR; +import static org.sonar.plugins.python.api.tree.Tree.Kind.LIST_COMPREHENSION; + +@Rule(key = "EC35") +@DeprecatedRuleKey(repositoryKey = "gci-python", ruleKey = "S34") +@DeprecatedRuleKey(repositoryKey = "gci-python", ruleKey = "EC34") +public class AvoidTryCatchFileOpenCheck 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() : ""; + } + +} diff --git a/src/main/java/fr/greencodeinitiative/python/checks/AvoidTryCatchFinallyCheck.java b/src/main/java/fr/greencodeinitiative/python/checks/AvoidTryCatchFinallyCheck.java deleted file mode 100644 index 8854149..0000000 --- a/src/main/java/fr/greencodeinitiative/python/checks/AvoidTryCatchFinallyCheck.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * 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 . - */ -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.tree.Tree; -import org.sonar.plugins.python.api.tree.TryStatement; -import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey; - -@Rule(key = "EC34") -@DeprecatedRuleKey(repositoryKey = "gci-python", ruleKey = "S34") -public class AvoidTryCatchFinallyCheck extends PythonSubscriptionCheck { - - public static final String DESCRIPTION = "Avoid the use of try-catch"; - - @Override - public void initialize(Context context) { - context.registerSyntaxNodeConsumer(Tree.Kind.TRY_STMT, this::visitNode); - } - - public void visitNode(SubscriptionContext ctx) { - TryStatement tryStatement = (TryStatement) ctx.syntaxNode(); - ctx.addIssue(tryStatement.tryKeyword(), DESCRIPTION); - - } - -} diff --git a/src/test/java/fr/greencodeinitiative/python/checks/AvoidTryCatchFinallyCheckTest.java b/src/test/java/fr/greencodeinitiative/python/checks/AvoidTryCatchFinallyCheckTest.java index 0f1bc57..7954a1e 100644 --- a/src/test/java/fr/greencodeinitiative/python/checks/AvoidTryCatchFinallyCheckTest.java +++ b/src/test/java/fr/greencodeinitiative/python/checks/AvoidTryCatchFinallyCheckTest.java @@ -24,6 +24,6 @@ public class AvoidTryCatchFinallyCheckTest { @Test public void test() { - PythonCheckVerifier.verify("src/test/resources/checks/avoidTryCatchFinallyCheck.py", new AvoidTryCatchFinallyCheck()); + PythonCheckVerifier.verify("src/test/resources/checks/avoidTryCatchWithFileOpenCheck.py", new AvoidTryCatchFileOpenCheck()); } } diff --git a/src/test/resources/checks/avoidTryCatchFinallyCheck.py b/src/test/resources/checks/avoidTryCatchWithFileOpenCheck.py similarity index 75% rename from src/test/resources/checks/avoidTryCatchFinallyCheck.py rename to src/test/resources/checks/avoidTryCatchWithFileOpenCheck.py index cbd081e..ce3caeb 100644 --- a/src/test/resources/checks/avoidTryCatchFinallyCheck.py +++ b/src/test/resources/checks/avoidTryCatchWithFileOpenCheck.py @@ -6,7 +6,7 @@ def my_function(): x=0 - try: # Noncompliant {{Avoid the use of try-catch}} + try: print(x) except: print("Something went wrong") @@ -14,8 +14,8 @@ def my_function(): 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) From bcdb97a4d257110ef6838dd13049319ccd0bc5ff Mon Sep 17 00:00:00 2001 From: David DE CARVALHO Date: Sat, 21 Oct 2023 23:19:58 +0200 Subject: [PATCH 2/6] [PR 128-ecocode] replace EC34 by EC35 : typo --- src/test/resources/checks/avoidTryCatchWithFileOpenCheck.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/resources/checks/avoidTryCatchWithFileOpenCheck.py b/src/test/resources/checks/avoidTryCatchWithFileOpenCheck.py index ce3caeb..bb867ce 100644 --- a/src/test/resources/checks/avoidTryCatchWithFileOpenCheck.py +++ b/src/test/resources/checks/avoidTryCatchWithFileOpenCheck.py @@ -2,7 +2,6 @@ path = 'hello.txt' - def my_function(): x=0 From f34a5f815e244fb8141e1aeb74eb24eb7b9d3c4a Mon Sep 17 00:00:00 2001 From: David DE CARVALHO Date: Mon, 23 Oct 2023 11:48:38 +0200 Subject: [PATCH 3/6] [PR 128-ecocode] replace EC34 by EC35 : refacto naming --- .../fr/greencodeinitiative/python/PythonRuleRepository.java | 2 +- ...OpenCheck.java => AvoidTryCatchWithFileOpenedCheck.java} | 6 +----- ...kTest.java => AvoidTryCatchWithFileOpenedCheckTest.java} | 4 ++-- ...FileOpenCheck.py => avoidTryCatchWithFileOpenedCheck.py} | 0 4 files changed, 4 insertions(+), 8 deletions(-) rename src/main/java/fr/greencodeinitiative/python/checks/{AvoidTryCatchFileOpenCheck.java => AvoidTryCatchWithFileOpenedCheck.java} (93%) rename src/test/java/fr/greencodeinitiative/python/checks/{AvoidTryCatchFinallyCheckTest.java => AvoidTryCatchWithFileOpenedCheckTest.java} (89%) rename src/test/resources/checks/{avoidTryCatchWithFileOpenCheck.py => avoidTryCatchWithFileOpenedCheck.py} (100%) diff --git a/src/main/java/fr/greencodeinitiative/python/PythonRuleRepository.java b/src/main/java/fr/greencodeinitiative/python/PythonRuleRepository.java index 3202891..c9ba42d 100644 --- a/src/main/java/fr/greencodeinitiative/python/PythonRuleRepository.java +++ b/src/main/java/fr/greencodeinitiative/python/PythonRuleRepository.java @@ -59,7 +59,7 @@ public List checkClasses() { AvoidGettersAndSetters.class, AvoidGlobalVariableInFunctionCheck.class, AvoidSQLRequestInLoop.class, - AvoidTryCatchFileOpenCheck.class, + AvoidTryCatchWithFileOpenedCheck.class, AvoidUnoptimizedVectorImagesCheck.class, NoFunctionCallWhenDeclaringForLoop.class, AvoidFullSQLRequest.class, diff --git a/src/main/java/fr/greencodeinitiative/python/checks/AvoidTryCatchFileOpenCheck.java b/src/main/java/fr/greencodeinitiative/python/checks/AvoidTryCatchWithFileOpenedCheck.java similarity index 93% rename from src/main/java/fr/greencodeinitiative/python/checks/AvoidTryCatchFileOpenCheck.java rename to src/main/java/fr/greencodeinitiative/python/checks/AvoidTryCatchWithFileOpenedCheck.java index 36f31bd..939fc40 100644 --- a/src/main/java/fr/greencodeinitiative/python/checks/AvoidTryCatchFileOpenCheck.java +++ b/src/main/java/fr/greencodeinitiative/python/checks/AvoidTryCatchWithFileOpenedCheck.java @@ -24,16 +24,12 @@ import org.sonar.plugins.python.api.tree.*; import org.sonarsource.analyzer.commons.annotations.DeprecatedRuleKey; -import java.util.List; -import java.util.Objects; - import static org.sonar.plugins.python.api.tree.Tree.Kind.CALL_EXPR; -import static org.sonar.plugins.python.api.tree.Tree.Kind.LIST_COMPREHENSION; @Rule(key = "EC35") @DeprecatedRuleKey(repositoryKey = "gci-python", ruleKey = "S34") @DeprecatedRuleKey(repositoryKey = "gci-python", ruleKey = "EC34") -public class AvoidTryCatchFileOpenCheck extends PythonSubscriptionCheck { +public class AvoidTryCatchWithFileOpenedCheck extends PythonSubscriptionCheck { public static final String DESCRIPTION = "Avoid the use of try-catch with a file open in try block"; diff --git a/src/test/java/fr/greencodeinitiative/python/checks/AvoidTryCatchFinallyCheckTest.java b/src/test/java/fr/greencodeinitiative/python/checks/AvoidTryCatchWithFileOpenedCheckTest.java similarity index 89% rename from src/test/java/fr/greencodeinitiative/python/checks/AvoidTryCatchFinallyCheckTest.java rename to src/test/java/fr/greencodeinitiative/python/checks/AvoidTryCatchWithFileOpenedCheckTest.java index 7954a1e..cfaf969 100644 --- a/src/test/java/fr/greencodeinitiative/python/checks/AvoidTryCatchFinallyCheckTest.java +++ b/src/test/java/fr/greencodeinitiative/python/checks/AvoidTryCatchWithFileOpenedCheckTest.java @@ -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/avoidTryCatchWithFileOpenCheck.py", new AvoidTryCatchFileOpenCheck()); + PythonCheckVerifier.verify("src/test/resources/checks/avoidTryCatchWithFileOpenedCheck.py", new AvoidTryCatchWithFileOpenedCheck()); } } diff --git a/src/test/resources/checks/avoidTryCatchWithFileOpenCheck.py b/src/test/resources/checks/avoidTryCatchWithFileOpenedCheck.py similarity index 100% rename from src/test/resources/checks/avoidTryCatchWithFileOpenCheck.py rename to src/test/resources/checks/avoidTryCatchWithFileOpenedCheck.py From 2896e1fe96d568362b25cc959a645e22a2644301 Mon Sep 17 00:00:00 2001 From: David DE CARVALHO Date: Fri, 27 Oct 2023 21:29:00 +0200 Subject: [PATCH 4/6] update ecocode-rule-specifications version --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 70d6375..a00e3c2 100644 --- a/pom.xml +++ b/pom.xml @@ -59,7 +59,7 @@ 5.3.1 - 0.0.3 + 0.0.6 2.5.0.1358 From 09d896d5aae93ede9b79e4a11817e1f2147fd35d Mon Sep 17 00:00:00 2001 From: David DE CARVALHO Date: Sat, 28 Oct 2023 22:41:57 +0200 Subject: [PATCH 5/6] [PR 128-ecocode] replace EC34 by EC35 : update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9613eff..b0944e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 From 753df7bfc7bddfe5913d36835407ff857bfee8a5 Mon Sep 17 00:00:00 2001 From: David DE CARVALHO Date: Sat, 28 Oct 2023 22:57:22 +0200 Subject: [PATCH 6/6] [PR 128-ecocode] replace EC34 by EC35 : correction of smell --- .../python/checks/AvoidTryCatchWithFileOpenedCheck.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/main/java/fr/greencodeinitiative/python/checks/AvoidTryCatchWithFileOpenedCheck.java b/src/main/java/fr/greencodeinitiative/python/checks/AvoidTryCatchWithFileOpenedCheck.java index 939fc40..0f8ecc3 100644 --- a/src/main/java/fr/greencodeinitiative/python/checks/AvoidTryCatchWithFileOpenedCheck.java +++ b/src/main/java/fr/greencodeinitiative/python/checks/AvoidTryCatchWithFileOpenedCheck.java @@ -64,12 +64,8 @@ private void checkCallExpressionInChildren(SubscriptionContext context, Tree stm } private void visitCallExpression(SubscriptionContext context, CallExpression callExpression){ - switch (getFunctionNameFromCallExpression(callExpression)) { - case "open": - context.addIssue(callExpression.firstToken(), DESCRIPTION); - break; - default: - break; + if ("open".equals(getFunctionNameFromCallExpression(callExpression))) { + context.addIssue(callExpression.firstToken(), DESCRIPTION); } }