|
| 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.List; |
| 21 | +import java.util.OptionalInt; |
| 22 | +import org.sonar.check.Rule; |
| 23 | +import org.sonar.plugins.python.api.PythonSubscriptionCheck; |
| 24 | +import org.sonar.plugins.python.api.SubscriptionContext; |
| 25 | +import org.sonar.plugins.python.api.tree.Expression; |
| 26 | +import org.sonar.plugins.python.api.tree.FunctionDef; |
| 27 | +import org.sonar.plugins.python.api.tree.ReturnStatement; |
| 28 | +import org.sonar.plugins.python.api.tree.Tree; |
| 29 | +import org.sonar.plugins.python.api.tree.Tuple; |
| 30 | + |
| 31 | +@Rule(key = "S8495") |
| 32 | +public class InconsistentTupleReturnCheck extends PythonSubscriptionCheck { |
| 33 | + |
| 34 | + private static final String MESSAGE = "Refactor this function to always return tuples of the same length."; |
| 35 | + private static final String SECONDARY_MESSAGE = "Returns a %d-tuple."; |
| 36 | + |
| 37 | + @Override |
| 38 | + public void initialize(Context context) { |
| 39 | + context.registerSyntaxNodeConsumer(Tree.Kind.FUNCDEF, InconsistentTupleReturnCheck::checkFunction); |
| 40 | + } |
| 41 | + |
| 42 | + private static void checkFunction(SubscriptionContext ctx) { |
| 43 | + FunctionDef functionDef = (FunctionDef) ctx.syntaxNode(); |
| 44 | + |
| 45 | + ReturnCheckUtils.ReturnStmtCollector collector = ReturnCheckUtils.ReturnStmtCollector.collect(functionDef); |
| 46 | + |
| 47 | + if (collector.containsYield()) { |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + List<ReturnStatement> returnStmts = collector.getReturnStmts(); |
| 52 | + |
| 53 | + List<ReturnWithLength> tupleReturns = new ArrayList<>(); |
| 54 | + for (ReturnStatement returnStmt : returnStmts) { |
| 55 | + OptionalInt length = getTupleLengthIfTupleReturn(returnStmt); |
| 56 | + if (length.isPresent()) { |
| 57 | + tupleReturns.add(new ReturnWithLength(returnStmt, length.getAsInt())); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + if (tupleReturns.size() < 2) { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + int firstLength = tupleReturns.get(0).length; |
| 66 | + boolean allSame = tupleReturns.stream().allMatch(r -> r.length == firstLength); |
| 67 | + if (allSame) { |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + PreciseIssue issue = ctx.addIssue(functionDef.name(), MESSAGE); |
| 72 | + for (ReturnWithLength tupleReturn : tupleReturns) { |
| 73 | + issue.secondary(tupleReturn.returnStmt, String.format(SECONDARY_MESSAGE, tupleReturn.length)); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private static boolean containsUnpacking(List<Expression> exprs) { |
| 78 | + return exprs.stream().anyMatch(e -> e.is(Tree.Kind.UNPACKING_EXPR)); |
| 79 | + } |
| 80 | + |
| 81 | + private static OptionalInt getTupleLengthIfTupleReturn(ReturnStatement returnStmt) { |
| 82 | + List<Expression> expressions = returnStmt.expressions(); |
| 83 | + if (expressions.isEmpty()) { |
| 84 | + return OptionalInt.empty(); |
| 85 | + } |
| 86 | + if (expressions.size() > 1) { |
| 87 | + // Implicit tuple: return a, b — skip if any element is a star expression |
| 88 | + if (containsUnpacking(expressions)) { |
| 89 | + return OptionalInt.empty(); |
| 90 | + } |
| 91 | + return OptionalInt.of(expressions.size()); |
| 92 | + } |
| 93 | + // Single expression - check if it's an explicit tuple literal |
| 94 | + Expression expr = expressions.get(0); |
| 95 | + if (expr.is(Tree.Kind.TUPLE)) { |
| 96 | + Tuple tuple = (Tuple) expr; |
| 97 | + if (containsUnpacking(tuple.elements())) { |
| 98 | + return OptionalInt.empty(); |
| 99 | + } |
| 100 | + return OptionalInt.of(tuple.elements().size()); |
| 101 | + } |
| 102 | + return OptionalInt.empty(); |
| 103 | + } |
| 104 | + |
| 105 | + private static class ReturnWithLength { |
| 106 | + final ReturnStatement returnStmt; |
| 107 | + final int length; |
| 108 | + |
| 109 | + ReturnWithLength(ReturnStatement returnStmt, int length) { |
| 110 | + this.returnStmt = returnStmt; |
| 111 | + this.length = length; |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments