|
| 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.List; |
| 20 | +import java.util.Set; |
| 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.tree.Argument; |
| 25 | +import org.sonar.plugins.python.api.tree.CallExpression; |
| 26 | +import org.sonar.plugins.python.api.tree.Expression; |
| 27 | +import org.sonar.plugins.python.api.tree.Name; |
| 28 | +import org.sonar.plugins.python.api.tree.QualifiedExpression; |
| 29 | +import org.sonar.plugins.python.api.tree.RegularArgument; |
| 30 | +import org.sonar.plugins.python.api.tree.Tree; |
| 31 | +import org.sonar.plugins.python.api.types.v2.matchers.TypeMatcher; |
| 32 | +import org.sonar.plugins.python.api.types.v2.matchers.TypeMatchers; |
| 33 | +import org.sonar.python.tree.TreeUtils; |
| 34 | + |
| 35 | +@Rule(key = "S8508") |
| 36 | +public class MutableDefaultValueCheck extends PythonSubscriptionCheck { |
| 37 | + |
| 38 | + private static final String MESSAGE = "Replace this mutable value with an immutable default to avoid shared state."; |
| 39 | + |
| 40 | + private static final TypeMatcher IS_DICT_TYPE = TypeMatchers.isType("builtins.dict"); |
| 41 | + private static final TypeMatcher IS_CONTEXT_VAR = TypeMatchers.isType("contextvars.ContextVar"); |
| 42 | + private static final TypeMatcher IS_MUTABLE_CONSTRUCTOR = TypeMatchers.any( |
| 43 | + TypeMatchers.isType("builtins.list"), |
| 44 | + TypeMatchers.isType("builtins.dict"), |
| 45 | + TypeMatchers.isType("builtins.set"), |
| 46 | + TypeMatchers.isType("builtins.bytearray") |
| 47 | + ); |
| 48 | + |
| 49 | + @Override |
| 50 | + public void initialize(Context context) { |
| 51 | + context.registerSyntaxNodeConsumer(Tree.Kind.CALL_EXPR, MutableDefaultValueCheck::checkCall); |
| 52 | + } |
| 53 | + |
| 54 | + private static void checkCall(SubscriptionContext ctx) { |
| 55 | + CallExpression callExpression = (CallExpression) ctx.syntaxNode(); |
| 56 | + Expression callee = callExpression.callee(); |
| 57 | + if (isDictFromkeys(callee, ctx)) { |
| 58 | + checkDictFromkeys(ctx, callExpression); |
| 59 | + } else if (IS_CONTEXT_VAR.isTrueFor(callee, ctx)) { |
| 60 | + checkContextVar(ctx, callExpression); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private static boolean isDictFromkeys(Expression callee, SubscriptionContext ctx) { |
| 65 | + if (!(callee instanceof QualifiedExpression qualifiedExpr)) { |
| 66 | + return false; |
| 67 | + } |
| 68 | + return "fromkeys".equals(qualifiedExpr.name().name()) |
| 69 | + && IS_DICT_TYPE.isTrueFor(qualifiedExpr.qualifier(), ctx); |
| 70 | + } |
| 71 | + |
| 72 | + private static void checkDictFromkeys(SubscriptionContext ctx, CallExpression callExpression) { |
| 73 | + List<Argument> arguments = callExpression.arguments(); |
| 74 | + if (arguments.size() < 2) { |
| 75 | + return; |
| 76 | + } |
| 77 | + Argument secondArg = arguments.get(1); |
| 78 | + if (secondArg instanceof RegularArgument regularArg && regularArg.keywordArgument() == null) { |
| 79 | + checkAndReportIfMutable(regularArg.expression(), ctx); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private static void checkContextVar(SubscriptionContext ctx, CallExpression callExpression) { |
| 84 | + RegularArgument defaultArg = TreeUtils.argumentByKeyword("default", callExpression.arguments()); |
| 85 | + if (defaultArg != null) { |
| 86 | + checkAndReportIfMutable(defaultArg.expression(), ctx); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private static void checkAndReportIfMutable(Expression value, SubscriptionContext ctx) { |
| 91 | + if (isMutableValue(value, ctx)) { |
| 92 | + ctx.addIssue(value, MESSAGE); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + private static boolean isMutableValue(Expression expression, SubscriptionContext ctx) { |
| 97 | + if (expression.is(Tree.Kind.LIST_LITERAL) |
| 98 | + || expression.is(Tree.Kind.DICTIONARY_LITERAL) |
| 99 | + || expression.is(Tree.Kind.SET_LITERAL)) { |
| 100 | + return true; |
| 101 | + } |
| 102 | + if (expression instanceof CallExpression callExpr) { |
| 103 | + return IS_MUTABLE_CONSTRUCTOR.isTrueFor(callExpr.callee(), ctx); |
| 104 | + } |
| 105 | + if (expression instanceof Name name) { |
| 106 | + Set<Expression> values = ctx.valuesAtLocation(name); |
| 107 | + return !values.isEmpty() && values.stream().allMatch(v -> isMutableValue(v, ctx)); |
| 108 | + } |
| 109 | + return false; |
| 110 | + } |
| 111 | +} |
0 commit comments