-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from green-code-initiative/EC34-replace
EC34 replaced by EC35 : python implementation
- Loading branch information
Showing
7 changed files
with
85 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 0 additions & 44 deletions
44
src/main/java/fr/greencodeinitiative/python/checks/AvoidTryCatchFinallyCheck.java
This file was deleted.
Oops, something went wrong.
77 changes: 77 additions & 0 deletions
77
src/main/java/fr/greencodeinitiative/python/checks/AvoidTryCatchWithFileOpenedCheck.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() : ""; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters