|
| 1 | +/* |
| 2 | + * SonarQube Python Plugin |
| 3 | + * Copyright (C) 2011-2020 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; |
| 21 | + |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Optional; |
| 25 | +import java.util.stream.Collectors; |
| 26 | +import java.util.stream.Stream; |
| 27 | +import org.sonar.check.Rule; |
| 28 | +import org.sonar.check.RuleProperty; |
| 29 | +import org.sonar.plugins.python.api.PythonSubscriptionCheck; |
| 30 | +import org.sonar.plugins.python.api.SubscriptionContext; |
| 31 | +import org.sonar.plugins.python.api.symbols.ClassSymbol; |
| 32 | +import org.sonar.plugins.python.api.symbols.FunctionSymbol; |
| 33 | +import org.sonar.plugins.python.api.symbols.Symbol; |
| 34 | +import org.sonar.plugins.python.api.tree.BinaryExpression; |
| 35 | +import org.sonar.plugins.python.api.tree.ClassDef; |
| 36 | +import org.sonar.plugins.python.api.tree.FileInput; |
| 37 | +import org.sonar.plugins.python.api.tree.FunctionDef; |
| 38 | +import org.sonar.plugins.python.api.tree.Name; |
| 39 | +import org.sonar.plugins.python.api.tree.QualifiedExpression; |
| 40 | +import org.sonar.plugins.python.api.tree.StringLiteral; |
| 41 | +import org.sonar.plugins.python.api.tree.Token; |
| 42 | +import org.sonar.plugins.python.api.tree.Tree; |
| 43 | +import org.sonar.plugins.python.api.tree.Tree.Kind; |
| 44 | +import org.sonar.plugins.python.api.tree.UnaryExpression; |
| 45 | +import org.sonar.python.tree.TreeUtils; |
| 46 | + |
| 47 | +@Rule(key = "S905") |
| 48 | +public class UselessStatementCheck extends PythonSubscriptionCheck { |
| 49 | + |
| 50 | + private static final boolean DEFAULT_REPORT_ON_STRINGS = false; |
| 51 | + |
| 52 | + @RuleProperty( |
| 53 | + key = "reportOnStrings", |
| 54 | + description = "Enable issues on unread attributes with a single underscore prefix", |
| 55 | + defaultValue = "" + DEFAULT_REPORT_ON_STRINGS) |
| 56 | + public boolean reportOnStrings = DEFAULT_REPORT_ON_STRINGS; |
| 57 | + |
| 58 | + @RuleProperty( |
| 59 | + key = "ignoredOperators", |
| 60 | + description = "Comma separated list of ignored operators", |
| 61 | + defaultValue = "") |
| 62 | + public String ignoredOperators = ""; |
| 63 | + |
| 64 | + List<String> ignoredOperatorsList; |
| 65 | + |
| 66 | + private List<String> ignoredOperators() { |
| 67 | + if (ignoredOperatorsList == null) { |
| 68 | + ignoredOperatorsList = Stream.of(ignoredOperators.split(",")) |
| 69 | + .map(String::trim).collect(Collectors.toList()); |
| 70 | + } |
| 71 | + return ignoredOperatorsList; |
| 72 | + } |
| 73 | + |
| 74 | + private static final List<Kind> regularKinds = Arrays.asList(Kind.NUMERIC_LITERAL, Kind.LIST_LITERAL, Kind.SET_LITERAL, Kind.DICTIONARY_LITERAL, |
| 75 | + Kind.NONE, Kind.CONDITIONAL_EXPR, Kind.LAMBDA); |
| 76 | + |
| 77 | + private static final List<Kind> binaryExpressionKinds = Arrays.asList(Kind.AND, Kind.OR, Kind.PLUS, Kind.MINUS, |
| 78 | + Kind.MULTIPLICATION, Kind.DIVISION, Kind.FLOOR_DIVISION, Kind.MODULO, Kind.MATRIX_MULTIPLICATION, Kind.SHIFT_EXPR, |
| 79 | + Kind.BITWISE_AND, Kind.BITWISE_OR, Kind.BITWISE_XOR, Kind.COMPARISON, Kind.POWER); |
| 80 | + |
| 81 | + private static final List<Kind> unaryExpressionKinds = Arrays.asList(Kind.UNARY_PLUS, Kind.UNARY_MINUS, Kind.BITWISE_COMPLEMENT, Kind.NOT); |
| 82 | + |
| 83 | + private static final String MESSAGE = "Remove or refactor this statement; it has no side effects."; |
| 84 | + |
| 85 | + @Override |
| 86 | + public void initialize(Context context) { |
| 87 | + context.registerSyntaxNodeConsumer(Kind.STRING_LITERAL, this::checkStringLiteral); |
| 88 | + context.registerSyntaxNodeConsumer(Kind.NAME, UselessStatementCheck::checkName); |
| 89 | + context.registerSyntaxNodeConsumer(Kind.QUALIFIED_EXPR, UselessStatementCheck::checkQualifiedExpression); |
| 90 | + binaryExpressionKinds.forEach(b -> context.registerSyntaxNodeConsumer(b, this::checkBinaryExpression)); |
| 91 | + unaryExpressionKinds.forEach(u -> context.registerSyntaxNodeConsumer(u, this::checkUnaryExpression)); |
| 92 | + regularKinds.forEach(r -> context.registerSyntaxNodeConsumer(r, UselessStatementCheck::checkNode)); |
| 93 | + } |
| 94 | + |
| 95 | + private static void checkNode(SubscriptionContext ctx) { |
| 96 | + Tree tree = ctx.syntaxNode(); |
| 97 | + Tree tryParent = TreeUtils.firstAncestorOfKind(tree, Kind.TRY_STMT); |
| 98 | + if (tryParent != null) { |
| 99 | + return; |
| 100 | + } |
| 101 | + if (isBooleanExpressionWithCalls(tree)) { |
| 102 | + return; |
| 103 | + } |
| 104 | + Tree parent = tree.parent(); |
| 105 | + if (parent == null || !parent.is(Kind.EXPRESSION_STMT)) { |
| 106 | + return; |
| 107 | + } |
| 108 | + ctx.addIssue(tree, MESSAGE); |
| 109 | + } |
| 110 | + |
| 111 | + private static boolean isBooleanExpressionWithCalls(Tree tree) { |
| 112 | + return (tree.is(Kind.AND) || tree.is(Kind.OR) || tree.is(Kind.NOT)) && (TreeUtils.hasDescendant(tree, t -> t.is(Kind.CALL_EXPR))); |
| 113 | + } |
| 114 | + |
| 115 | + private void checkStringLiteral(SubscriptionContext ctx) { |
| 116 | + StringLiteral stringLiteral = (StringLiteral) ctx.syntaxNode(); |
| 117 | + if (!reportOnStrings || isDocString(stringLiteral)) { |
| 118 | + return; |
| 119 | + } |
| 120 | + checkNode(ctx); |
| 121 | + } |
| 122 | + |
| 123 | + private static void checkName(SubscriptionContext ctx) { |
| 124 | + Name name = (Name) ctx.syntaxNode(); |
| 125 | + Symbol symbol = name.symbol(); |
| 126 | + if (symbol != null && symbol.is(Symbol.Kind.CLASS)) { |
| 127 | + ClassSymbol classSymbol = (ClassSymbol) symbol; |
| 128 | + // Creating an exception without raising it is covered by S3984 |
| 129 | + if (classSymbol.canBeOrExtend("BaseException")) { |
| 130 | + return; |
| 131 | + } |
| 132 | + } |
| 133 | + checkNode(ctx); |
| 134 | + } |
| 135 | + |
| 136 | + private static void checkQualifiedExpression(SubscriptionContext ctx) { |
| 137 | + QualifiedExpression qualifiedExpression = (QualifiedExpression) ctx.syntaxNode(); |
| 138 | + Symbol symbol = qualifiedExpression.symbol(); |
| 139 | + if (symbol != null && symbol.is(Symbol.Kind.FUNCTION) && ((FunctionSymbol) symbol).decorators().stream().noneMatch(d -> d.matches("property"))) { |
| 140 | + checkNode(ctx); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + private void checkBinaryExpression(SubscriptionContext ctx) { |
| 145 | + BinaryExpression binaryExpression = (BinaryExpression) ctx.syntaxNode(); |
| 146 | + Token operator = binaryExpression.operator(); |
| 147 | + if (ignoredOperators().contains(operator.value())) { |
| 148 | + return; |
| 149 | + } |
| 150 | + checkNode(ctx); |
| 151 | + } |
| 152 | + |
| 153 | + private void checkUnaryExpression(SubscriptionContext ctx) { |
| 154 | + UnaryExpression unaryExpression = (UnaryExpression) ctx.syntaxNode(); |
| 155 | + Token operator = unaryExpression.operator(); |
| 156 | + if (ignoredOperators().contains(operator.value())) { |
| 157 | + return; |
| 158 | + } |
| 159 | + checkNode(ctx); |
| 160 | + } |
| 161 | + |
| 162 | + private static boolean isDocString(StringLiteral stringLiteral) { |
| 163 | + Tree parent = TreeUtils.firstAncestorOfKind(stringLiteral, Kind.FILE_INPUT, Kind.CLASSDEF, Kind.FUNCDEF); |
| 164 | + return Optional.ofNullable(parent) |
| 165 | + .map(p -> ((p.is(Kind.FILE_INPUT) && stringLiteral.equals(((FileInput) p).docstring())) |
| 166 | + || (p.is(Kind.CLASSDEF) && stringLiteral.equals(((ClassDef) p).docstring())) |
| 167 | + || (p.is(Kind.FUNCDEF) && stringLiteral.equals(((FunctionDef) p).docstring())))) |
| 168 | + .orElse(false); |
| 169 | + } |
| 170 | +} |
0 commit comments