|
| 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 | +} |
0 commit comments