|
| 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 org.sonar.check.Rule; |
| 20 | +import org.sonar.plugins.python.api.PythonSubscriptionCheck; |
| 21 | +import org.sonar.plugins.python.api.SubscriptionContext; |
| 22 | +import org.sonar.plugins.python.api.quickfix.PythonQuickFix; |
| 23 | +import org.sonar.plugins.python.api.symbols.v2.SymbolV2; |
| 24 | +import org.sonar.plugins.python.api.tree.CallExpression; |
| 25 | +import org.sonar.plugins.python.api.tree.Expression; |
| 26 | +import org.sonar.plugins.python.api.tree.ExpressionStatement; |
| 27 | +import org.sonar.plugins.python.api.tree.ForStatement; |
| 28 | +import org.sonar.plugins.python.api.tree.Name; |
| 29 | +import org.sonar.plugins.python.api.tree.QualifiedExpression; |
| 30 | +import org.sonar.plugins.python.api.tree.RegularArgument; |
| 31 | +import org.sonar.plugins.python.api.tree.Tree; |
| 32 | +import org.sonar.plugins.python.api.tree.UnpackingExpression; |
| 33 | +import org.sonar.plugins.python.api.types.v2.matchers.TypeMatcher; |
| 34 | +import org.sonar.plugins.python.api.types.v2.matchers.TypeMatchers; |
| 35 | +import org.sonar.python.quickfix.TextEditUtils; |
| 36 | +import org.sonar.python.tree.TreeUtils; |
| 37 | + |
| 38 | +@Rule(key = "S8502") |
| 39 | +public class SetUpdateOverForLoopCheck extends PythonSubscriptionCheck { |
| 40 | + |
| 41 | + private static final TypeMatcher SET_TYPE_MATCHER = TypeMatchers.isObjectOfType("builtins.set"); |
| 42 | + |
| 43 | + private static final String MESSAGE = "Use \"set.update()\" instead of a for-loop with \"add()\"."; |
| 44 | + |
| 45 | + @Override |
| 46 | + public void initialize(Context context) { |
| 47 | + context.registerSyntaxNodeConsumer(Tree.Kind.FOR_STMT, SetUpdateOverForLoopCheck::checkForStatement); |
| 48 | + } |
| 49 | + |
| 50 | + private static void checkForStatement(SubscriptionContext ctx) { |
| 51 | + ForStatement forStatement = (ForStatement) ctx.syntaxNode(); |
| 52 | + |
| 53 | + if (forStatement.expressions().size() != 1) { |
| 54 | + return; |
| 55 | + } |
| 56 | + if (forStatement.elseClause() != null) { |
| 57 | + return; |
| 58 | + } |
| 59 | + Expression loopVarExpr = forStatement.expressions().get(0); |
| 60 | + if (!(loopVarExpr instanceof Name loopVar)) { |
| 61 | + return; |
| 62 | + } |
| 63 | + if (forStatement.testExpressions().size() != 1) { |
| 64 | + return; |
| 65 | + } |
| 66 | + Expression iterable = forStatement.testExpressions().get(0); |
| 67 | + |
| 68 | + CallExpression callExpr = extractSingleBodyCallExpression(forStatement); |
| 69 | + if (callExpr == null) { |
| 70 | + return; |
| 71 | + } |
| 72 | + if (!(callExpr.callee() instanceof QualifiedExpression qualifiedExpr)) { |
| 73 | + return; |
| 74 | + } |
| 75 | + // TypeMatchers operate on types, not method names; the callee name must be checked syntactically |
| 76 | + if (!"add".equals(qualifiedExpr.name().name())) { |
| 77 | + return; |
| 78 | + } |
| 79 | + Expression receiver = qualifiedExpr.qualifier(); |
| 80 | + |
| 81 | + // Exclude transient objects like get_set().add(item) |
| 82 | + if (receiver instanceof CallExpression) { |
| 83 | + return; |
| 84 | + } |
| 85 | + if (!SET_TYPE_MATCHER.isTrueFor(receiver, ctx)) { |
| 86 | + return; |
| 87 | + } |
| 88 | + if (!isLoopVariableTheOnlyArgument(callExpr, loopVar)) { |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + PreciseIssue issue = ctx.addIssue(qualifiedExpr, MESSAGE); |
| 93 | + addQuickFix(issue, forStatement, receiver, iterable); |
| 94 | + } |
| 95 | + |
| 96 | + private static boolean isLoopVariableTheOnlyArgument(CallExpression callExpr, Name loopVar) { |
| 97 | + if (callExpr.arguments().size() != 1) { |
| 98 | + return false; |
| 99 | + } |
| 100 | + if (!(callExpr.arguments().get(0) instanceof RegularArgument regularArgument)) { |
| 101 | + return false; |
| 102 | + } |
| 103 | + if (regularArgument.keywordArgument() != null) { |
| 104 | + return false; |
| 105 | + } |
| 106 | + Expression argExpr = regularArgument.expression(); |
| 107 | + if (argExpr instanceof UnpackingExpression || !(argExpr instanceof Name argName)) { |
| 108 | + return false; |
| 109 | + } |
| 110 | + SymbolV2 loopVarSymbol = loopVar.symbolV2(); |
| 111 | + return loopVarSymbol != null && loopVarSymbol.equals(argName.symbolV2()); |
| 112 | + } |
| 113 | + |
| 114 | + private static CallExpression extractSingleBodyCallExpression(ForStatement forStatement) { |
| 115 | + if (forStatement.body().statements().size() != 1) { |
| 116 | + return null; |
| 117 | + } |
| 118 | + if (!(forStatement.body().statements().get(0) instanceof ExpressionStatement exprStmt)) { |
| 119 | + return null; |
| 120 | + } |
| 121 | + if (exprStmt.expressions().size() != 1) { |
| 122 | + return null; |
| 123 | + } |
| 124 | + if (!(exprStmt.expressions().get(0) instanceof CallExpression callExpr)) { |
| 125 | + return null; |
| 126 | + } |
| 127 | + return callExpr; |
| 128 | + } |
| 129 | + |
| 130 | + private static void addQuickFix(PreciseIssue issue, ForStatement forStatement, Expression receiver, Expression iterable) { |
| 131 | + String receiverText = TreeUtils.treeToString(receiver, false); |
| 132 | + String iterableText = TreeUtils.treeToString(iterable, false); |
| 133 | + if (receiverText == null || iterableText == null) { |
| 134 | + return; |
| 135 | + } |
| 136 | + |
| 137 | + String replacement = receiverText + ".update(" + iterableText + ")"; |
| 138 | + |
| 139 | + PythonQuickFix quickFix = PythonQuickFix.newQuickFix("Use \"set.update()\" instead", TextEditUtils.replace(forStatement, replacement)); |
| 140 | + issue.addQuickFix(quickFix); |
| 141 | + } |
| 142 | +} |
0 commit comments