|
| 1 | +/* |
| 2 | + * SonarQube Python Plugin |
| 3 | + * Copyright (C) 2011-2025 SonarSource Sàrl |
| 4 | + * mailto:info AT sonarsource DOT com |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA. |
| 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.ArrayList; |
| 20 | +import java.util.LinkedHashMap; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.Optional; |
| 24 | +import org.sonar.check.Rule; |
| 25 | +import org.sonar.plugins.python.api.PythonSubscriptionCheck; |
| 26 | +import org.sonar.plugins.python.api.SubscriptionContext; |
| 27 | +import org.sonar.plugins.python.api.tree.AnnotatedAssignment; |
| 28 | +import org.sonar.plugins.python.api.tree.AssignmentStatement; |
| 29 | +import org.sonar.plugins.python.api.tree.ClassDef; |
| 30 | +import org.sonar.plugins.python.api.tree.Expression; |
| 31 | +import org.sonar.plugins.python.api.tree.ExpressionList; |
| 32 | +import org.sonar.plugins.python.api.tree.Name; |
| 33 | +import org.sonar.plugins.python.api.tree.Statement; |
| 34 | +import org.sonar.plugins.python.api.tree.Tree; |
| 35 | +import org.sonar.plugins.python.api.tree.Tree.Kind; |
| 36 | + |
| 37 | +@Rule(key = "S8512") |
| 38 | +public class ClassFieldDefinedMultipleTimesCheck extends PythonSubscriptionCheck { |
| 39 | + |
| 40 | + @Override |
| 41 | + public void initialize(Context context) { |
| 42 | + context.registerSyntaxNodeConsumer(Kind.CLASSDEF, ctx -> checkClass(ctx, (ClassDef) ctx.syntaxNode())); |
| 43 | + } |
| 44 | + |
| 45 | + private static void checkClass(SubscriptionContext ctx, ClassDef classDef) { |
| 46 | + Map<String, List<Tree>> fieldDefinitions = new LinkedHashMap<>(); |
| 47 | + |
| 48 | + for (Statement stmt : classDef.body().statements()) { |
| 49 | + collectFieldDefinition(stmt, fieldDefinitions); |
| 50 | + } |
| 51 | + |
| 52 | + reportDuplicateDefinitions(ctx, fieldDefinitions); |
| 53 | + } |
| 54 | + |
| 55 | + private static void collectFieldDefinition(Statement stmt, Map<String, List<Tree>> fieldDefinitions) { |
| 56 | + if (stmt.is(Kind.ASSIGNMENT_STMT)) { |
| 57 | + getAssignmentName((AssignmentStatement) stmt) |
| 58 | + .ifPresent(name -> fieldDefinitions.computeIfAbsent(name.name(), k -> new ArrayList<>()).add(name)); |
| 59 | + } else if (stmt.is(Kind.ANNOTATED_ASSIGNMENT)) { |
| 60 | + getAnnotatedAssignmentName((AnnotatedAssignment) stmt) |
| 61 | + .ifPresent(name -> fieldDefinitions.computeIfAbsent(name.name(), k -> new ArrayList<>()).add(name)); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + private static Optional<Name> getAssignmentName(AssignmentStatement assignment) { |
| 66 | + List<ExpressionList> lhsList = assignment.lhsExpressions(); |
| 67 | + if (lhsList.size() != 1) { |
| 68 | + return Optional.empty(); |
| 69 | + } |
| 70 | + List<Expression> expressions = lhsList.get(0).expressions(); |
| 71 | + if (expressions.size() == 1 && expressions.get(0).is(Kind.NAME)) { |
| 72 | + return Optional.of((Name) expressions.get(0)); |
| 73 | + } |
| 74 | + return Optional.empty(); |
| 75 | + } |
| 76 | + |
| 77 | + private static Optional<Name> getAnnotatedAssignmentName(AnnotatedAssignment annotated) { |
| 78 | + if (annotated.variable().is(Kind.NAME) && annotated.assignedValue() != null) { |
| 79 | + return Optional.of((Name) annotated.variable()); |
| 80 | + } |
| 81 | + return Optional.empty(); |
| 82 | + } |
| 83 | + |
| 84 | + private static void reportDuplicateDefinitions(SubscriptionContext ctx, Map<String, List<Tree>> fieldDefinitions) { |
| 85 | + fieldDefinitions.forEach((name, definitions) -> { |
| 86 | + for (int i = 0; i < definitions.size() - 1; i++) { |
| 87 | + Tree current = definitions.get(i); |
| 88 | + Tree next = definitions.get(i + 1); |
| 89 | + String message = String.format("Remove this assignment; \"%s\" is assigned again on line %d.", name, next.firstToken().line()); |
| 90 | + ctx.addIssue(current, message).secondary(next, "Reassignment."); |
| 91 | + } |
| 92 | + }); |
| 93 | + } |
| 94 | +} |
0 commit comments