Search in sources :

Example 11 with StringLiteral

use of org.sonar.plugins.python.api.tree.StringLiteral in project sonar-python by SonarSource.

the class AbstractStringFormatCheck method checkPrintfStyle.

protected void checkPrintfStyle(SubscriptionContext ctx) {
    BinaryExpression expression = (BinaryExpression) ctx.syntaxNode();
    StringLiteral literal = extractStringLiteral(expression.leftOperand());
    if (literal == null) {
        return;
    }
    if (literal.stringElements().stream().anyMatch(AbstractStringFormatCheck::isFStringOrBytesLiteral)) {
        // Do not bother with byte formatting and f-strings for now.
        return;
    }
    this.checkPrintfStyle(ctx, expression, literal);
}
Also used : BinaryExpression(org.sonar.plugins.python.api.tree.BinaryExpression) StringLiteral(org.sonar.plugins.python.api.tree.StringLiteral)

Example 12 with StringLiteral

use of org.sonar.plugins.python.api.tree.StringLiteral in project sonar-python by SonarSource.

the class AbstractStringFormatCheck method checkStrFormatStyle.

protected void checkStrFormatStyle(SubscriptionContext ctx) {
    CallExpression callExpression = (CallExpression) ctx.syntaxNode();
    if (!isQualifiedCallToStrFormat(callExpression)) {
        return;
    }
    Expression qualifier = ((QualifiedExpression) callExpression.callee()).qualifier();
    StringLiteral literal = extractStringLiteral(qualifier);
    if (literal == null) {
        return;
    }
    if (literal.stringElements().stream().anyMatch(AbstractStringFormatCheck::isFStringOrBytesLiteral)) {
        // Avoid raising on f-strings
        return;
    }
    this.checkStrFormatStyle(ctx, callExpression, qualifier, literal);
}
Also used : QualifiedExpression(org.sonar.plugins.python.api.tree.QualifiedExpression) StringLiteral(org.sonar.plugins.python.api.tree.StringLiteral) BinaryExpression(org.sonar.plugins.python.api.tree.BinaryExpression) CallExpression(org.sonar.plugins.python.api.tree.CallExpression) QualifiedExpression(org.sonar.plugins.python.api.tree.QualifiedExpression) Expression(org.sonar.plugins.python.api.tree.Expression) CallExpression(org.sonar.plugins.python.api.tree.CallExpression)

Example 13 with StringLiteral

use of org.sonar.plugins.python.api.tree.StringLiteral in project sonar-python by SonarSource.

the class ImplicitStringConcatenationCheck method initialize.

@Override
public void initialize(Context context) {
    context.registerSyntaxNodeConsumer(Tree.Kind.STRING_LITERAL, ctx -> {
        StringLiteral stringLiteral = (StringLiteral) ctx.syntaxNode();
        if (stringLiteral.parent().is(Tree.Kind.MODULO, Tree.Kind.QUALIFIED_EXPR)) {
            // if string formatting is used, explicit string concatenation with "+" might fail
            return;
        }
        if (stringLiteral.stringElements().size() == 1) {
            return;
        }
        checkStringLiteral(stringLiteral, ctx);
    });
}
Also used : StringLiteral(org.sonar.plugins.python.api.tree.StringLiteral)

Example 14 with StringLiteral

use of org.sonar.plugins.python.api.tree.StringLiteral in project sonar-python by SonarSource.

the class DocstringExtractorTest method assertDocstring.

private void assertDocstring(Tree.Kind kind, int line, String expectedDocstring) {
    StringLiteral docString = getDocstring(kind, line);
    assertThat(docString).as("docstring for AstNode of type " + kind + " at line " + line).isNotNull();
    assertThat(docString.trimmedQuotesValue()).isEqualTo(expectedDocstring);
}
Also used : StringLiteral(org.sonar.plugins.python.api.tree.StringLiteral)

Example 15 with StringLiteral

use of org.sonar.plugins.python.api.tree.StringLiteral in project sonar-python by SonarSource.

the class UselessStatementCheck method checkStringLiteral.

private void checkStringLiteral(SubscriptionContext ctx) {
    StringLiteral stringLiteral = (StringLiteral) ctx.syntaxNode();
    if (!reportOnStrings || isDocString(stringLiteral)) {
        return;
    }
    checkNode(ctx);
}
Also used : StringLiteral(org.sonar.plugins.python.api.tree.StringLiteral)

Aggregations

StringLiteral (org.sonar.plugins.python.api.tree.StringLiteral)27 CallExpression (org.sonar.plugins.python.api.tree.CallExpression)15 Expression (org.sonar.plugins.python.api.tree.Expression)15 BinaryExpression (org.sonar.plugins.python.api.tree.BinaryExpression)12 List (java.util.List)9 QualifiedExpression (org.sonar.plugins.python.api.tree.QualifiedExpression)9 SubscriptionExpression (org.sonar.plugins.python.api.tree.SubscriptionExpression)8 Tree (org.sonar.plugins.python.api.tree.Tree)8 AssignmentExpression (org.sonar.plugins.python.api.tree.AssignmentExpression)7 RegularArgument (org.sonar.plugins.python.api.tree.RegularArgument)7 SliceExpression (org.sonar.plugins.python.api.tree.SliceExpression)7 Rule (org.sonar.check.Rule)6 Symbol (org.sonar.plugins.python.api.symbols.Symbol)6 AwaitExpression (org.sonar.plugins.python.api.tree.AwaitExpression)6 ComprehensionExpression (org.sonar.plugins.python.api.tree.ComprehensionExpression)6 ConditionalExpression (org.sonar.plugins.python.api.tree.ConditionalExpression)6 DictCompExpression (org.sonar.plugins.python.api.tree.DictCompExpression)6 EllipsisExpression (org.sonar.plugins.python.api.tree.EllipsisExpression)6 FormattedExpression (org.sonar.plugins.python.api.tree.FormattedExpression)6 InExpression (org.sonar.plugins.python.api.tree.InExpression)6