|
| 1 | +/* |
| 2 | + * SonarQube Python Plugin |
| 3 | + * Copyright (C) 2011-2022 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 GNU Lesser General Public |
| 8 | + * License as published by the Free Software Foundation; either |
| 9 | + * version 3 of the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + * Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public License |
| 17 | + * along with this program; if not, write to the Free Software Foundation, |
| 18 | + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | + */ |
| 20 | +package org.sonar.python.checks.tests; |
| 21 | + |
| 22 | +import java.util.List; |
| 23 | +import java.util.Set; |
| 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.symbols.FunctionSymbol; |
| 28 | +import org.sonar.plugins.python.api.symbols.Symbol; |
| 29 | +import org.sonar.plugins.python.api.tree.Argument; |
| 30 | +import org.sonar.plugins.python.api.tree.AssertStatement; |
| 31 | +import org.sonar.plugins.python.api.tree.CallExpression; |
| 32 | +import org.sonar.plugins.python.api.tree.Expression; |
| 33 | +import org.sonar.plugins.python.api.tree.HasSymbol; |
| 34 | +import org.sonar.plugins.python.api.tree.Name; |
| 35 | +import org.sonar.plugins.python.api.tree.NumericLiteral; |
| 36 | +import org.sonar.plugins.python.api.tree.RegularArgument; |
| 37 | +import org.sonar.plugins.python.api.tree.Tree; |
| 38 | +import org.sonar.python.cfg.fixpoint.ReachingDefinitionsAnalysis; |
| 39 | +import org.sonar.python.checks.CheckUtils; |
| 40 | +import org.sonar.python.tree.TreeUtils; |
| 41 | + |
| 42 | +import static org.sonar.plugins.python.api.tree.Tree.Kind.NAME; |
| 43 | +import static org.sonar.plugins.python.api.tree.Tree.Kind.NUMERIC_LITERAL; |
| 44 | +import static org.sonar.plugins.python.api.tree.Tree.Kind.QUALIFIED_EXPR; |
| 45 | +import static org.sonar.python.checks.CheckUtils.isConstant; |
| 46 | + |
| 47 | +@Rule(key = "S5914") |
| 48 | +public class UnconditionalAssertionCheck extends PythonSubscriptionCheck { |
| 49 | + |
| 50 | + private static final List<String> BOOLEAN_ASSERTIONS = List.of("assertTrue", "assertFalse"); |
| 51 | + private static final List<String> NONE_ASSERTIONS = List.of("assertIsNone", "assertIsNotNone"); |
| 52 | + private static final List<String> IS_ASSERTIONS = List.of("assertIs", "assertIsNot"); |
| 53 | + |
| 54 | + private static final String BOOLEAN_MESSAGE = "Replace this expression; its boolean value is constant."; |
| 55 | + private static final String NONE_MESSAGE = "Remove this identity assertion; its value is constant."; |
| 56 | + private static final String IS_MESSAGE = "Replace this \"assertIs\" call with an \"assertEqual\" call."; |
| 57 | + private static final String IS_NOT_MESSAGE = "Replace this \"assertIsNot\" call with an \"assertNotEqual\" call."; |
| 58 | + private static final String IS_SECONDARY_MESSAGE = "This expression creates a new object every time."; |
| 59 | + |
| 60 | + private static final List<String> ACCEPTED_DECORATORS = List.of("overload", "staticmethod", "classmethod"); |
| 61 | + |
| 62 | + private ReachingDefinitionsAnalysis reachingDefinitionsAnalysis; |
| 63 | + |
| 64 | + @Override |
| 65 | + public void initialize(Context context) { |
| 66 | + context.registerSyntaxNodeConsumer(Tree.Kind.FILE_INPUT, ctx -> |
| 67 | + reachingDefinitionsAnalysis = new ReachingDefinitionsAnalysis(ctx.pythonFile())); |
| 68 | + |
| 69 | + context.registerSyntaxNodeConsumer(Tree.Kind.ASSERT_STMT, ctx -> { |
| 70 | + AssertStatement assertStatement = (AssertStatement) ctx.syntaxNode(); |
| 71 | + Expression condition = assertStatement.condition(); |
| 72 | + if (!condition.is(Tree.Kind.TUPLE) && !isFalseOrZeroLiteral(condition) && CheckUtils.isConstant(condition)) { |
| 73 | + ctx.addIssue(condition, BOOLEAN_MESSAGE); |
| 74 | + } |
| 75 | + }); |
| 76 | + |
| 77 | + context.registerSyntaxNodeConsumer(Tree.Kind.CALL_EXPR, ctx -> { |
| 78 | + CallExpression call = (CallExpression) ctx.syntaxNode(); |
| 79 | + Symbol symbol = call.calleeSymbol(); |
| 80 | + if (symbol == null) { |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + String name = symbol.name(); |
| 85 | + List<Argument> arguments = call.arguments(); |
| 86 | + |
| 87 | + if (BOOLEAN_ASSERTIONS.contains(name)) { |
| 88 | + checkBooleanAssertion(ctx, TreeUtils.nthArgumentOrKeyword(0, "expr", arguments)); |
| 89 | + } else if (NONE_ASSERTIONS.contains(name)) { |
| 90 | + checkNoneAssertion(ctx, call, TreeUtils.nthArgumentOrKeyword(0, "expr", arguments)); |
| 91 | + } else if (IS_ASSERTIONS.contains(name)) { |
| 92 | + String message = "assertIs".equals(name) ? IS_MESSAGE : IS_NOT_MESSAGE; |
| 93 | + checkIsAssertion(ctx, call, TreeUtils.nthArgumentOrKeyword(0, "first", arguments), message); |
| 94 | + checkIsAssertion(ctx, call, TreeUtils.nthArgumentOrKeyword(1, "second", arguments), message); |
| 95 | + } |
| 96 | + }); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * `assert False` or `assert 0` is often used to make a test fail. |
| 101 | + * Usually it is better to use another assertion or throw an AssertionException. |
| 102 | + * However, this rule is not intended to check this best practice. |
| 103 | + */ |
| 104 | + private static boolean isFalseOrZeroLiteral(Expression expression) { |
| 105 | + if (expression.is(NAME)) { |
| 106 | + return "False".equals(((Name) expression).name()); |
| 107 | + } |
| 108 | + if (expression.is(NUMERIC_LITERAL)) { |
| 109 | + return ((NumericLiteral) expression).valueAsLong() == 0; |
| 110 | + } |
| 111 | + return false; |
| 112 | + } |
| 113 | + |
| 114 | + private void checkNoneAssertion(SubscriptionContext ctx, CallExpression call, RegularArgument arg) { |
| 115 | + if (isUnconditional(arg)) { |
| 116 | + ctx.addIssue(call, NONE_MESSAGE); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + private void checkBooleanAssertion(SubscriptionContext ctx, RegularArgument arg) { |
| 121 | + if (isUnconditional(arg)) { |
| 122 | + ctx.addIssue(arg, BOOLEAN_MESSAGE); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + private static void checkIsAssertion(SubscriptionContext ctx, CallExpression call, RegularArgument arg, String message) { |
| 127 | + if (CheckUtils.isConstantCollectionLiteral(arg.expression())) { |
| 128 | + ctx.addIssue(call.callee(), message).secondary(arg, IS_SECONDARY_MESSAGE); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + private boolean isUnconditional(RegularArgument argument) { |
| 133 | + Expression expression = argument.expression(); |
| 134 | + if (isConstant(expression)) { |
| 135 | + return true; |
| 136 | + } |
| 137 | + |
| 138 | + if (expression.is(NAME) || expression.is(QUALIFIED_EXPR)) { |
| 139 | + Symbol symbol = ((HasSymbol) expression).symbol(); |
| 140 | + if (symbol != null && isClassOrFunction(symbol)) { |
| 141 | + return true; |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + if (expression.is(NAME)) { |
| 146 | + Set<Expression> valuesAtLocation = reachingDefinitionsAnalysis.valuesAtLocation(((Name) expression)); |
| 147 | + if (valuesAtLocation.size() == 1) { |
| 148 | + return CheckUtils.isImmutableConstant(valuesAtLocation.iterator().next()); |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + return false; |
| 153 | + } |
| 154 | + |
| 155 | + private static boolean isClassOrFunction(Symbol symbol) { |
| 156 | + if (symbol.is(Symbol.Kind.CLASS)) { |
| 157 | + return true; |
| 158 | + } |
| 159 | + if (symbol.is(Symbol.Kind.FUNCTION)) { |
| 160 | + // Avoid potential FPs with properties: only report on limited selection of "safe" decorators |
| 161 | + return ACCEPTED_DECORATORS.containsAll(((FunctionSymbol) symbol).decorators()); |
| 162 | + } |
| 163 | + return false; |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public CheckScope scope() { |
| 168 | + return CheckScope.ALL; |
| 169 | + } |
| 170 | +} |
0 commit comments