|
| 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 org.sonar.check.Rule; |
| 24 | +import org.sonar.plugins.python.api.PythonSubscriptionCheck; |
| 25 | +import org.sonar.plugins.python.api.SubscriptionContext; |
| 26 | +import org.sonar.plugins.python.api.tree.Expression; |
| 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.Tree; |
| 30 | +import org.sonar.plugins.python.api.tree.Tuple; |
| 31 | + |
| 32 | +@Rule(key = "S8510") |
| 33 | +public class NestedLoopVariableReuseCheck extends PythonSubscriptionCheck { |
| 34 | + |
| 35 | + private static final String MESSAGE = "Rename this loop variable; it shadows the outer loop variable \"%s\"."; |
| 36 | + private static final String SECONDARY_MESSAGE = "Outer loop variable."; |
| 37 | + |
| 38 | + @Override |
| 39 | + public void initialize(Context context) { |
| 40 | + context.registerSyntaxNodeConsumer(Tree.Kind.FOR_STMT, NestedLoopVariableReuseCheck::checkForStatement); |
| 41 | + } |
| 42 | + |
| 43 | + private static void checkForStatement(SubscriptionContext ctx) { |
| 44 | + ForStatement forStatement = (ForStatement) ctx.syntaxNode(); |
| 45 | + List<Name> innerVarNames = extractLoopVarNames(forStatement); |
| 46 | + if (innerVarNames.isEmpty()) { |
| 47 | + return; |
| 48 | + } |
| 49 | + Map<Name, List<Name>> shadowedOuterNames = collectShadowedNames(forStatement, innerVarNames); |
| 50 | + reportIssues(ctx, shadowedOuterNames); |
| 51 | + } |
| 52 | + |
| 53 | + private static Map<Name, List<Name>> collectShadowedNames(ForStatement forStatement, List<Name> innerVarNames) { |
| 54 | + Map<Name, List<Name>> shadowedOuterNames = new LinkedHashMap<>(); |
| 55 | + for (Name innerName : innerVarNames) { |
| 56 | + shadowedOuterNames.put(innerName, new ArrayList<>()); |
| 57 | + } |
| 58 | + Tree current = forStatement.parent(); |
| 59 | + while (current != null) { |
| 60 | + if (current.is(Tree.Kind.FUNCDEF, Tree.Kind.CLASSDEF, Tree.Kind.LAMBDA, |
| 61 | + Tree.Kind.LIST_COMPREHENSION, Tree.Kind.SET_COMPREHENSION, |
| 62 | + Tree.Kind.DICT_COMPREHENSION, Tree.Kind.GENERATOR_EXPR)) { |
| 63 | + break; |
| 64 | + } |
| 65 | + if (current.is(Tree.Kind.FOR_STMT)) { |
| 66 | + collectMatchingNames((ForStatement) current, innerVarNames, shadowedOuterNames); |
| 67 | + } |
| 68 | + current = current.parent(); |
| 69 | + } |
| 70 | + return shadowedOuterNames; |
| 71 | + } |
| 72 | + |
| 73 | + private static void collectMatchingNames(ForStatement outerLoop, List<Name> innerVarNames, Map<Name, List<Name>> shadowedOuterNames) { |
| 74 | + List<Name> outerVarNames = extractLoopVarNames(outerLoop); |
| 75 | + for (Name innerName : innerVarNames) { |
| 76 | + for (Name outerName : outerVarNames) { |
| 77 | + if (innerName.name().equals(outerName.name())) { |
| 78 | + shadowedOuterNames.get(innerName).add(outerName); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private static void reportIssues(SubscriptionContext ctx, Map<Name, List<Name>> shadowedOuterNames) { |
| 85 | + for (Map.Entry<Name, List<Name>> entry : shadowedOuterNames.entrySet()) { |
| 86 | + Name innerName = entry.getKey(); |
| 87 | + List<Name> outerNames = entry.getValue(); |
| 88 | + if (!outerNames.isEmpty()) { |
| 89 | + var issue = ctx.addIssue(innerName, String.format(MESSAGE, innerName.name())); |
| 90 | + for (Name outerName : outerNames) { |
| 91 | + issue.secondary(outerName, SECONDARY_MESSAGE); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private static List<Name> extractLoopVarNames(ForStatement forStatement) { |
| 98 | + List<Name> names = new ArrayList<>(); |
| 99 | + for (Expression expr : forStatement.expressions()) { |
| 100 | + collectNames(expr, names); |
| 101 | + } |
| 102 | + return names; |
| 103 | + } |
| 104 | + |
| 105 | + private static void collectNames(Expression expr, List<Name> names) { |
| 106 | + if (expr.is(Tree.Kind.NAME)) { |
| 107 | + Name name = (Name) expr; |
| 108 | + if (!name.name().startsWith("_")) { |
| 109 | + names.add(name); |
| 110 | + } |
| 111 | + } else if (expr.is(Tree.Kind.TUPLE)) { |
| 112 | + for (Expression element : ((Tuple) expr).elements()) { |
| 113 | + collectNames(element, names); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments