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);
}
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);
}
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);
});
}
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);
}
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);
}
Aggregations