|
| 1 | +/* |
| 2 | + * SonarQube Python Plugin |
| 3 | + * Copyright (C) SonarSource Sàrl |
| 4 | + * mailto:info AT sonarsource DOT com |
| 5 | + * |
| 6 | + * You can redistribute and/or modify this program under the terms of |
| 7 | + * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | + * See the Sonar Source-Available License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the Sonar Source-Available License |
| 15 | + * along with this program; if not, see https://sonarsource.com/license/ssal/ |
| 16 | + */ |
| 17 | +package org.sonar.python.checks; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | +import java.util.Optional; |
| 21 | +import org.sonar.check.Rule; |
| 22 | +import org.sonar.plugins.python.api.PythonSubscriptionCheck; |
| 23 | +import org.sonar.plugins.python.api.SubscriptionContext; |
| 24 | +import org.sonar.plugins.python.api.quickfix.PythonQuickFix; |
| 25 | +import org.sonar.plugins.python.api.tree.Argument; |
| 26 | +import org.sonar.plugins.python.api.tree.CallExpression; |
| 27 | +import org.sonar.plugins.python.api.tree.ExpressionStatement; |
| 28 | +import org.sonar.plugins.python.api.tree.IfStatement; |
| 29 | +import org.sonar.plugins.python.api.tree.InExpression; |
| 30 | +import org.sonar.plugins.python.api.tree.QualifiedExpression; |
| 31 | +import org.sonar.plugins.python.api.tree.RegularArgument; |
| 32 | +import org.sonar.plugins.python.api.tree.Statement; |
| 33 | +import org.sonar.plugins.python.api.tree.Tree; |
| 34 | +import org.sonar.plugins.python.api.types.v2.matchers.TypeMatcher; |
| 35 | +import org.sonar.plugins.python.api.types.v2.matchers.TypeMatchers; |
| 36 | +import org.sonar.python.checks.utils.CheckUtils; |
| 37 | +import org.sonar.python.quickfix.TextEditUtils; |
| 38 | +import org.sonar.python.tree.TreeUtils; |
| 39 | + |
| 40 | +@Rule(key = "S8492") |
| 41 | +public class SetDiscardOverRemoveCheck extends PythonSubscriptionCheck { |
| 42 | + |
| 43 | + private static final String MESSAGE = "Use \"discard()\" instead of checking membership before calling \"remove()\"."; |
| 44 | + private static final String QUICK_FIX_MESSAGE = "Replace with \"discard()\""; |
| 45 | + private static final String REMOVE_METHOD_NAME = "remove"; |
| 46 | + private static final TypeMatcher SET_TYPE_MATCHER = TypeMatchers.isObjectInstanceOf("builtins.set"); |
| 47 | + |
| 48 | + @Override |
| 49 | + public void initialize(Context context) { |
| 50 | + context.registerSyntaxNodeConsumer(Tree.Kind.IF_STMT, SetDiscardOverRemoveCheck::checkIfStatement); |
| 51 | + } |
| 52 | + |
| 53 | + private static void checkIfStatement(SubscriptionContext ctx) { |
| 54 | + IfStatement ifStatement = (IfStatement) ctx.syntaxNode(); |
| 55 | + if (ifStatement.isElif() |
| 56 | + || ifStatement.elseBranch() != null |
| 57 | + || !ifStatement.elifBranches().isEmpty()) { |
| 58 | + return; |
| 59 | + } |
| 60 | + if (!(ifStatement.condition() instanceof InExpression inExpression) || inExpression.notToken() != null) { |
| 61 | + return; |
| 62 | + } |
| 63 | + List<Statement> statements = ifStatement.body().statements(); |
| 64 | + if (statements.size() != 1 || !(statements.get(0) instanceof ExpressionStatement expressionStatement)) { |
| 65 | + return; |
| 66 | + } |
| 67 | + if (expressionStatement.expressions().size() != 1 |
| 68 | + || !(expressionStatement.expressions().get(0) instanceof CallExpression callExpression)) { |
| 69 | + return; |
| 70 | + } |
| 71 | + if (!(callExpression.callee() instanceof QualifiedExpression qualifiedExpression) |
| 72 | + || !REMOVE_METHOD_NAME.equals(qualifiedExpression.name().name())) { |
| 73 | + return; |
| 74 | + } |
| 75 | + List<Argument> arguments = callExpression.arguments(); |
| 76 | + if (arguments.size() != 1 || !(arguments.get(0) instanceof RegularArgument regularArgument) |
| 77 | + || regularArgument.keywordArgument() != null) { |
| 78 | + return; |
| 79 | + } |
| 80 | + if (!CheckUtils.areEquivalent(qualifiedExpression.qualifier(), inExpression.rightOperand()) |
| 81 | + || !CheckUtils.areEquivalent(regularArgument.expression(), inExpression.leftOperand())) { |
| 82 | + return; |
| 83 | + } |
| 84 | + if (!SET_TYPE_MATCHER.isTrueFor(qualifiedExpression.qualifier(), ctx)) { |
| 85 | + return; |
| 86 | + } |
| 87 | + var issue = ctx.addIssue(inExpression, MESSAGE); |
| 88 | + createQuickFix(ifStatement, qualifiedExpression, regularArgument).ifPresent(issue::addQuickFix); |
| 89 | + } |
| 90 | + |
| 91 | + private static Optional<PythonQuickFix> createQuickFix(IfStatement ifStatement, QualifiedExpression qualifiedExpression, RegularArgument argument) { |
| 92 | + String qualifierText = TreeUtils.treeToString(qualifiedExpression.qualifier(), false); |
| 93 | + String argumentText = TreeUtils.treeToString(argument.expression(), false); |
| 94 | + if (qualifierText == null || argumentText == null) { |
| 95 | + return Optional.empty(); |
| 96 | + } |
| 97 | + String replacement = "%s.discard(%s)".formatted(qualifierText, argumentText); |
| 98 | + return Optional.of(PythonQuickFix.newQuickFix(QUICK_FIX_MESSAGE) |
| 99 | + .addTextEdit(TextEditUtils.replace(ifStatement, replacement)) |
| 100 | + .build()); |
| 101 | + } |
| 102 | +} |
0 commit comments