|
| 1 | +/* |
| 2 | + * SonarQube Python Plugin |
| 3 | + * Copyright (C) SonarSource Sàrl |
| 4 | + * mailto:info AT sonarsource DOT com |
| 5 | + * |
| 6 | + * You can redistribute and/or modify this program under the terms of |
| 7 | + * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. |
| 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.Arrays; |
| 20 | +import java.util.HashSet; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Locale; |
| 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.tree.BinaryExpression; |
| 28 | +import org.sonar.plugins.python.api.tree.CallExpression; |
| 29 | +import org.sonar.plugins.python.api.tree.DictionaryLiteral; |
| 30 | +import org.sonar.plugins.python.api.tree.DictionaryLiteralElement; |
| 31 | +import org.sonar.plugins.python.api.tree.Expression; |
| 32 | +import org.sonar.plugins.python.api.tree.KeyValuePair; |
| 33 | +import org.sonar.plugins.python.api.tree.RegularArgument; |
| 34 | +import org.sonar.plugins.python.api.tree.StringLiteral; |
| 35 | +import org.sonar.plugins.python.api.tree.Tree; |
| 36 | +import org.sonar.plugins.python.api.types.v2.matchers.TypeMatcher; |
| 37 | +import org.sonar.plugins.python.api.types.v2.matchers.TypeMatchers; |
| 38 | +import org.sonar.python.tree.TreeUtils; |
| 39 | + |
| 40 | +@Rule(key = "S8554") |
| 41 | +public class LoggingBestPracticesCheck extends PythonSubscriptionCheck { |
| 42 | + |
| 43 | + private static final TypeMatcher LOGGING_CALL_MATCHER = TypeMatchers.any( |
| 44 | + TypeMatchers.isType("logging.debug"), |
| 45 | + TypeMatchers.isType("logging.info"), |
| 46 | + TypeMatchers.isType("logging.warning"), |
| 47 | + TypeMatchers.isType("logging.warn"), |
| 48 | + TypeMatchers.isType("logging.error"), |
| 49 | + TypeMatchers.isType("logging.exception"), |
| 50 | + TypeMatchers.isType("logging.critical"), |
| 51 | + TypeMatchers.isType("logging.Logger.debug"), |
| 52 | + TypeMatchers.isType("logging.Logger.info"), |
| 53 | + TypeMatchers.isType("logging.Logger.warning"), |
| 54 | + TypeMatchers.isType("logging.Logger.warn"), |
| 55 | + TypeMatchers.isType("logging.Logger.error"), |
| 56 | + TypeMatchers.isType("logging.Logger.exception"), |
| 57 | + TypeMatchers.isType("logging.Logger.critical") |
| 58 | + ); |
| 59 | + |
| 60 | + private static final TypeMatcher DEPRECATED_WARN_MATCHER = TypeMatchers.any( |
| 61 | + TypeMatchers.isType("logging.warn"), |
| 62 | + TypeMatchers.isType("logging.Logger.warn") |
| 63 | + ); |
| 64 | + |
| 65 | + private static final TypeMatcher STR_FORMAT_MATCHER = TypeMatchers.isType("str.format"); |
| 66 | + |
| 67 | + private static final Set<String> LOG_RECORD_ATTRIBUTES = new HashSet<>(Arrays.asList( |
| 68 | + "name", "msg", "args", "created", "filename", "funcName", "levelname", "levelno", |
| 69 | + "lineno", "module", "msecs", "message", "pathname", "process", "processName", |
| 70 | + "relativeCreated", "thread", "threadName", "exc_info", "exc_text", "stack_info", |
| 71 | + "taskName", "asctime" |
| 72 | + )); |
| 73 | + |
| 74 | + private static final String EAGER_FORMAT_MESSAGE = |
| 75 | + "Pass formatting arguments to the logging call instead of pre-formatting the message string."; |
| 76 | + private static final String DEPRECATED_WARN_MESSAGE = |
| 77 | + "Use \"warning\" instead of the deprecated \"warn\" method."; |
| 78 | + private static final String EXTRA_COLLISION_MESSAGE = |
| 79 | + "Remove or rename this key; it overrides a built-in LogRecord attribute."; |
| 80 | + |
| 81 | + @Override |
| 82 | + public void initialize(Context context) { |
| 83 | + context.registerSyntaxNodeConsumer(Tree.Kind.CALL_EXPR, LoggingBestPracticesCheck::checkLoggingCall); |
| 84 | + } |
| 85 | + |
| 86 | + private static void checkLoggingCall(SubscriptionContext ctx) { |
| 87 | + CallExpression call = (CallExpression) ctx.syntaxNode(); |
| 88 | + if (!LOGGING_CALL_MATCHER.isTrueFor(call.callee(), ctx)) { |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + if (DEPRECATED_WARN_MATCHER.isTrueFor(call.callee(), ctx)) { |
| 93 | + ctx.addIssue(call.callee(), DEPRECATED_WARN_MESSAGE); |
| 94 | + } |
| 95 | + |
| 96 | + List<RegularArgument> positionalArgs = call.arguments().stream() |
| 97 | + .flatMap(TreeUtils.toStreamInstanceOfMapper(RegularArgument.class)) |
| 98 | + .filter(arg -> arg.keywordArgument() == null) |
| 99 | + .toList(); |
| 100 | + |
| 101 | + if (!positionalArgs.isEmpty()) { |
| 102 | + checkEagerFormatting(ctx, positionalArgs.get(0).expression()); |
| 103 | + } |
| 104 | + |
| 105 | + checkExtraAttributeCollision(ctx, call); |
| 106 | + } |
| 107 | + |
| 108 | + private static void checkEagerFormatting(SubscriptionContext ctx, Expression expr) { |
| 109 | + if (expr instanceof StringLiteral literal) { |
| 110 | + boolean isFString = literal.stringElements().stream() |
| 111 | + .anyMatch(e -> e.prefix().toLowerCase(Locale.ENGLISH).contains("f") && !e.formattedExpressions().isEmpty()); |
| 112 | + if (isFString) { |
| 113 | + ctx.addIssue(expr, EAGER_FORMAT_MESSAGE); |
| 114 | + } |
| 115 | + } else if (expr instanceof CallExpression innerCall && STR_FORMAT_MATCHER.isTrueFor(innerCall.callee(), ctx)) { |
| 116 | + ctx.addIssue(expr, EAGER_FORMAT_MESSAGE); |
| 117 | + } else if (expr.is(Tree.Kind.MODULO) && containsStringLiteral(((BinaryExpression) expr).leftOperand())) { |
| 118 | + ctx.addIssue(expr, EAGER_FORMAT_MESSAGE); |
| 119 | + } else if (expr.is(Tree.Kind.PLUS) && containsStringLiteral(expr)) { |
| 120 | + ctx.addIssue(expr, EAGER_FORMAT_MESSAGE); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + private static boolean containsStringLiteral(Expression expr) { |
| 125 | + if (expr instanceof StringLiteral) { |
| 126 | + return true; |
| 127 | + } |
| 128 | + if (expr.is(Tree.Kind.PLUS)) { |
| 129 | + BinaryExpression plus = (BinaryExpression) expr; |
| 130 | + return containsStringLiteral(plus.leftOperand()) || containsStringLiteral(plus.rightOperand()); |
| 131 | + } |
| 132 | + return false; |
| 133 | + } |
| 134 | + |
| 135 | + private static void checkExtraAttributeCollision(SubscriptionContext ctx, CallExpression call) { |
| 136 | + RegularArgument extraArg = TreeUtils.argumentByKeyword("extra", call.arguments()); |
| 137 | + if (extraArg == null || !(extraArg.expression() instanceof DictionaryLiteral dict)) { |
| 138 | + return; |
| 139 | + } |
| 140 | + for (DictionaryLiteralElement element : dict.elements()) { |
| 141 | + if (element instanceof KeyValuePair kvp |
| 142 | + && kvp.key() instanceof StringLiteral key |
| 143 | + && LOG_RECORD_ATTRIBUTES.contains(key.trimmedQuotesValue())) { |
| 144 | + ctx.addIssue(key, EXTRA_COLLISION_MESSAGE); |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments