use of org.sonar.plugins.python.api.tree.QualifiedExpression in project sonar-python by SonarSource.
the class DebugModeCheck method initialize.
@Override
public void initialize(Context context) {
context.registerSyntaxNodeConsumer(Kind.CALL_EXPR, ctx -> {
CallExpression callExpression = (CallExpression) ctx.syntaxNode();
List<Argument> arguments = callExpression.arguments();
if (!(callExpression.callee() instanceof QualifiedExpression)) {
return;
}
if ("django.conf.settings.configure".equals(getQualifiedName(callExpression)) && !arguments.isEmpty()) {
arguments.stream().filter(DebugModeCheck::isDebugArgument).forEach(arg -> ctx.addIssue(arg, MESSAGE));
}
});
context.registerSyntaxNodeConsumer(Kind.ASSIGNMENT_STMT, ctx -> {
if (!settingFiles.contains(ctx.pythonFile().fileName())) {
return;
}
AssignmentStatement assignmentStatementTree = (AssignmentStatement) ctx.syntaxNode();
for (ExpressionList lhsExpression : assignmentStatementTree.lhsExpressions()) {
boolean isDebugProperties = lhsExpression.expressions().stream().anyMatch(DebugModeCheck::isDebugIdentifier);
if (isDebugProperties && isTrueLiteral(assignmentStatementTree.assignedValue())) {
ctx.addIssue(assignmentStatementTree, MESSAGE);
}
}
});
}
use of org.sonar.plugins.python.api.tree.QualifiedExpression in project sonar-python by SonarSource.
the class StringFormatCorrectnessCheck method isQualifiedCallToLogger.
private static boolean isQualifiedCallToLogger(CallExpression callExpression) {
if (!callExpression.callee().is(Tree.Kind.QUALIFIED_EXPR)) {
return false;
}
QualifiedExpression qualifiedExpression = (QualifiedExpression) callExpression.callee();
if (!LOGGER_METHOD_NAMES.contains(qualifiedExpression.name().name())) {
return false;
}
Expression qualifier = qualifiedExpression.qualifier();
if (!qualifier.is(Tree.Kind.NAME)) {
return false;
}
Expression singleAssignedValue = Expressions.singleAssignedValue((Name) qualifier);
if (singleAssignedValue == null || !singleAssignedValue.is(Tree.Kind.CALL_EXPR)) {
return false;
}
CallExpression call = (CallExpression) singleAssignedValue;
Symbol symbol = call.calleeSymbol();
if (symbol == null) {
return false;
}
return "logging.getLogger".equals(symbol.fullyQualifiedName());
}
use of org.sonar.plugins.python.api.tree.QualifiedExpression in project sonar-python by SonarSource.
the class UselessStatementCheck method checkQualifiedExpression.
private static void checkQualifiedExpression(SubscriptionContext ctx) {
QualifiedExpression qualifiedExpression = (QualifiedExpression) ctx.syntaxNode();
Symbol symbol = qualifiedExpression.symbol();
if (symbol != null && symbol.is(Symbol.Kind.FUNCTION) && ((FunctionSymbol) symbol).decorators().stream().noneMatch(d -> d.matches("property"))) {
checkNode(ctx);
}
}
use of org.sonar.plugins.python.api.tree.QualifiedExpression in project sonar-python by SonarSource.
the class VerifiedSslTlsCertificateCheck method sslSetVerifyCheck.
/**
* Check for the <code>OpenSSL.SSL.Context.set_verify</code> flag settings.
*
* Searches for `set_verify` invocations on instances of `OpenSSL.SSL.Context`,
* extracts the flags from the first argument, checks that the combination of flags is secure.
*
* @param subscriptionContext the subscription context passed by <code>Context.registerSyntaxNodeConsumer</code>.
*/
private static void sslSetVerifyCheck(SubscriptionContext subscriptionContext) {
CallExpression callExpr = (CallExpression) subscriptionContext.syntaxNode();
boolean isSetVerifyInvocation = ofNullable(callExpr.calleeSymbol()).map(Symbol::fullyQualifiedName).filter(SET_VERIFY::equals).isPresent();
if (isSetVerifyInvocation) {
List<Argument> args = callExpr.arguments();
if (!args.isEmpty()) {
Tree flagsArgument = args.get(0);
if (flagsArgument.is(Tree.Kind.REGULAR_ARGUMENT)) {
Set<QualifiedExpression> flags = extractFlags(((RegularArgumentImpl) flagsArgument).expression());
checkFlagSettings(flags).ifPresent(issue -> subscriptionContext.addIssue(issue.token, MESSAGE));
}
}
}
}
use of org.sonar.plugins.python.api.tree.QualifiedExpression in project sonar-python by SonarSource.
the class DuplicateArgumentCheck method initialize.
@Override
public void initialize(Context context) {
context.registerSyntaxNodeConsumer(Tree.Kind.CALL_EXPR, ctx -> {
CallExpression callExpression = (CallExpression) ctx.syntaxNode();
Symbol symbol = callExpression.calleeSymbol();
if (symbol == null || !symbol.is(Symbol.Kind.FUNCTION)) {
return;
}
FunctionSymbol functionSymbol = (FunctionSymbol) symbol;
boolean isStaticCall = callExpression.callee().is(Tree.Kind.NAME) || Optional.of(callExpression.callee()).filter(c -> c.is(Tree.Kind.QUALIFIED_EXPR)).flatMap(q -> TreeUtils.getSymbolFromTree(((QualifiedExpression) q).qualifier()).filter(s -> s.is(Symbol.Kind.CLASS))).isPresent();
int firstParameterOffset = SymbolUtils.firstParameterOffset(functionSymbol, isStaticCall);
if (isException(functionSymbol) || firstParameterOffset == -1) {
return;
}
checkFunctionCall(callExpression, functionSymbol, firstParameterOffset, ctx);
});
}
Aggregations