|
| 1 | +/* |
| 2 | + * SonarQube Python Plugin |
| 3 | + * Copyright (C) 2011-2025 SonarSource SA |
| 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.EnumSet; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.Set; |
| 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.Argument; |
| 27 | +import org.sonar.plugins.python.api.tree.CallExpression; |
| 28 | +import org.sonar.plugins.python.api.tree.ComprehensionExpression; |
| 29 | +import org.sonar.plugins.python.api.tree.Expression; |
| 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.Tree.Kind; |
| 33 | +import org.sonar.python.checks.utils.IsComprehensionTransformedChecker; |
| 34 | +import org.sonar.python.types.v2.TypeCheckBuilder; |
| 35 | +import org.sonar.python.types.v2.TypeCheckMap; |
| 36 | + |
| 37 | +@Rule(key = "S7496") |
| 38 | +public class CollectionCreationWrappedInConstructorCheck extends PythonSubscriptionCheck { |
| 39 | + |
| 40 | + private static final String TUPLE = "tuple"; |
| 41 | + private static final String SET = "set"; |
| 42 | + private static final String LIST = "list"; |
| 43 | + private static final String DICT = "dict"; |
| 44 | + |
| 45 | + private static final Set<String> COLLECTION_TYPES = Set.of(LIST, SET, DICT, TUPLE); |
| 46 | + |
| 47 | + private static final Set<Kind> COLLECTION_KINDS = EnumSet.of( |
| 48 | + Kind.LIST_LITERAL, Kind.SET_LITERAL, Kind.DICTIONARY_LITERAL, Kind.TUPLE, |
| 49 | + Kind.LIST_COMPREHENSION, Kind.SET_COMPREHENSION, Kind.DICT_COMPREHENSION |
| 50 | + ); |
| 51 | + |
| 52 | + private static final Map<String, List<Kind>> SAME_INNER_TYPES_MAP = Map.of( |
| 53 | + LIST, List.of(Kind.LIST_LITERAL, Kind.LIST_COMPREHENSION), |
| 54 | + SET, List.of(Kind.SET_LITERAL, Kind.SET_COMPREHENSION), |
| 55 | + DICT, List.of(Kind.DICTIONARY_LITERAL, Kind.DICT_COMPREHENSION), |
| 56 | + TUPLE, List.of(Kind.TUPLE) |
| 57 | + ); |
| 58 | + |
| 59 | + private final TypeCheckMap<String> collectionTypeCheckerMap = new TypeCheckMap<>(); |
| 60 | + private IsComprehensionTransformedChecker isComprehensionTransformedChecker = null; |
| 61 | + |
| 62 | + @Override |
| 63 | + public void initialize(Context context) { |
| 64 | + isComprehensionTransformedChecker = new IsComprehensionTransformedChecker(context); |
| 65 | + context.registerSyntaxNodeConsumer(Tree.Kind.FILE_INPUT, this::initializeTypeCheckers); |
| 66 | + context.registerSyntaxNodeConsumer(Kind.CALL_EXPR, this::checkCalls); |
| 67 | + } |
| 68 | + |
| 69 | + private void initializeTypeCheckers(SubscriptionContext ctx) { |
| 70 | + for (var collectionType : COLLECTION_TYPES) { |
| 71 | + TypeCheckBuilder typeChecker = ctx.typeChecker().typeCheckBuilder().isTypeWithFqn(collectionType); |
| 72 | + collectionTypeCheckerMap.put(typeChecker, collectionType); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private void checkCalls(SubscriptionContext ctx) { |
| 77 | + CallExpression callExpression = (CallExpression) ctx.syntaxNode(); |
| 78 | + Expression callee = callExpression.callee(); |
| 79 | + RegularArgument regularArgument = getSingleRegularArgument(callExpression); |
| 80 | + if (regularArgument == null) { |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + if (regularArgument.expression() instanceof ComprehensionExpression comprehensionExpression |
| 85 | + && !isComprehensionTransformedChecker.isGeneratorTransformingData(comprehensionExpression, callee.typeV2())) { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + var collectionType = collectionTypeCheckerMap.getForType(callee.typeV2()); |
| 90 | + if (collectionType == null) { |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + if (isSameInnerType(collectionType, regularArgument)) { |
| 95 | + ctx.addIssue(callExpression.callee(), getSameInnerTypeCollectionMessage(collectionType)); |
| 96 | + } else if (isSensitiveDifferentLiteralInnerType(collectionType, regularArgument)) { |
| 97 | + ctx.addIssue(callExpression.callee(), getDifferentInnerTypeCollectionMessage(collectionType)); |
| 98 | + } else if (collectionType.equals(TUPLE)) { |
| 99 | + handleTupleConstructorSpecialCases(ctx, regularArgument); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private static RegularArgument getSingleRegularArgument(CallExpression callExpression) { |
| 104 | + if (callExpression.arguments().size() != 1) { |
| 105 | + return null; |
| 106 | + } |
| 107 | + Argument argument = callExpression.arguments().get(0); |
| 108 | + if (!(argument instanceof RegularArgument regularArgument)) { |
| 109 | + return null; |
| 110 | + } |
| 111 | + return regularArgument; |
| 112 | + } |
| 113 | + |
| 114 | + private static boolean isSameInnerType(String collectionType, RegularArgument regularArgument) { |
| 115 | + return SAME_INNER_TYPES_MAP.get(collectionType) |
| 116 | + .stream() |
| 117 | + .anyMatch(kind -> regularArgument.expression().is(kind)); |
| 118 | + } |
| 119 | + |
| 120 | + private static boolean isSensitiveDifferentLiteralInnerType(String collectionType, RegularArgument regularArgument) { |
| 121 | + if (!isCollectionKind(regularArgument)) { |
| 122 | + return false; |
| 123 | + } |
| 124 | + |
| 125 | + if (isSameInnerType(collectionType, regularArgument)) { |
| 126 | + return false; |
| 127 | + } |
| 128 | + |
| 129 | + // It is common and valid to check for duplicates using set comprehension before creating another collection |
| 130 | + if (regularArgument.expression().is(Kind.SET_COMPREHENSION)) { |
| 131 | + return false; |
| 132 | + } |
| 133 | + |
| 134 | + // When tuple are created from comprehensions, the issue is handled in handleTupleConstructorSpecialCases as it requires a special message |
| 135 | + EnumSet<Kind> comprehensions = EnumSet.of(Kind.LIST_COMPREHENSION, Kind.SET_COMPREHENSION, Kind.DICT_COMPREHENSION); |
| 136 | + return !collectionType.equals(TUPLE) || !comprehensions.contains(regularArgument.expression().getKind()); |
| 137 | + } |
| 138 | + |
| 139 | + private static String getSameInnerTypeCollectionMessage(String type) { |
| 140 | + return "Remove the redundant " + type + " constructor call."; |
| 141 | + } |
| 142 | + |
| 143 | + private static String getDifferentInnerTypeCollectionMessage(String type) { |
| 144 | + return "Replace this " + type + " constructor call by a " + type + " literal."; |
| 145 | + } |
| 146 | + |
| 147 | + private static void handleTupleConstructorSpecialCases(SubscriptionContext ctx, RegularArgument regularArgument) { |
| 148 | + if (regularArgument.expression().is(Kind.LIST_COMPREHENSION)) { |
| 149 | + ctx.addIssue(regularArgument, "Replace this list comprehension by a generator."); |
| 150 | + } else if (regularArgument.expression().is(Kind.SET_COMPREHENSION)) { |
| 151 | + ctx.addIssue(regularArgument, "Replace this set comprehension by a generator."); |
| 152 | + } else if (regularArgument.expression().is(Kind.DICT_COMPREHENSION)) { |
| 153 | + ctx.addIssue(regularArgument, "Replace this dict comprehension by a generator."); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + private static boolean isCollectionKind(RegularArgument regularArgument) { |
| 158 | + return COLLECTION_KINDS.contains(regularArgument.expression().getKind()); |
| 159 | + } |
| 160 | +} |
0 commit comments