Search in sources :

Example 1 with QualifiedExpression

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);
            }
        }
    });
}
Also used : RegularArgument(org.sonar.plugins.python.api.tree.RegularArgument) Argument(org.sonar.plugins.python.api.tree.Argument) QualifiedExpression(org.sonar.plugins.python.api.tree.QualifiedExpression) AssignmentStatement(org.sonar.plugins.python.api.tree.AssignmentStatement) CallExpression(org.sonar.plugins.python.api.tree.CallExpression) ExpressionList(org.sonar.plugins.python.api.tree.ExpressionList)

Example 2 with QualifiedExpression

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());
}
Also used : QualifiedExpression(org.sonar.plugins.python.api.tree.QualifiedExpression) 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) Symbol(org.sonar.plugins.python.api.symbols.Symbol) CallExpression(org.sonar.plugins.python.api.tree.CallExpression)

Example 3 with QualifiedExpression

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);
    }
}
Also used : Arrays(java.util.Arrays) RuleProperty(org.sonar.check.RuleProperty) PythonSubscriptionCheck(org.sonar.plugins.python.api.PythonSubscriptionCheck) FunctionDef(org.sonar.plugins.python.api.tree.FunctionDef) ClassDef(org.sonar.plugins.python.api.tree.ClassDef) WithStatement(org.sonar.plugins.python.api.tree.WithStatement) TreeUtils(org.sonar.python.tree.TreeUtils) Kind(org.sonar.plugins.python.api.tree.Tree.Kind) Name(org.sonar.plugins.python.api.tree.Name) QualifiedExpression(org.sonar.plugins.python.api.tree.QualifiedExpression) WithItem(org.sonar.plugins.python.api.tree.WithItem) BinaryExpression(org.sonar.plugins.python.api.tree.BinaryExpression) Usage(org.sonar.plugins.python.api.symbols.Usage) ConditionalExpression(org.sonar.plugins.python.api.tree.ConditionalExpression) StringLiteral(org.sonar.plugins.python.api.tree.StringLiteral) Token(org.sonar.plugins.python.api.tree.Token) SubscriptionContext(org.sonar.plugins.python.api.SubscriptionContext) ClassSymbol(org.sonar.plugins.python.api.symbols.ClassSymbol) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) UnaryExpression(org.sonar.plugins.python.api.tree.UnaryExpression) List(java.util.List) Stream(java.util.stream.Stream) CallExpression(org.sonar.plugins.python.api.tree.CallExpression) FileInput(org.sonar.plugins.python.api.tree.FileInput) Optional(java.util.Optional) Tree(org.sonar.plugins.python.api.tree.Tree) Rule(org.sonar.check.Rule) FunctionSymbol(org.sonar.plugins.python.api.symbols.FunctionSymbol) Symbol(org.sonar.plugins.python.api.symbols.Symbol) QualifiedExpression(org.sonar.plugins.python.api.tree.QualifiedExpression) ClassSymbol(org.sonar.plugins.python.api.symbols.ClassSymbol) FunctionSymbol(org.sonar.plugins.python.api.symbols.FunctionSymbol) Symbol(org.sonar.plugins.python.api.symbols.Symbol)

Example 4 with QualifiedExpression

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));
            }
        }
    }
}
Also used : RegularArgument(org.sonar.plugins.python.api.tree.RegularArgument) Argument(org.sonar.plugins.python.api.tree.Argument) QualifiedExpression(org.sonar.plugins.python.api.tree.QualifiedExpression) Symbol(org.sonar.plugins.python.api.symbols.Symbol) Tree(org.sonar.plugins.python.api.tree.Tree) CallExpression(org.sonar.plugins.python.api.tree.CallExpression)

Example 5 with QualifiedExpression

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);
    });
}
Also used : PythonFile(org.sonar.plugins.python.api.PythonFile) RegularArgument(org.sonar.plugins.python.api.tree.RegularArgument) PythonSubscriptionCheck(org.sonar.plugins.python.api.PythonSubscriptionCheck) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TreeUtils(org.sonar.python.tree.TreeUtils) UnpackingExpression(org.sonar.plugins.python.api.tree.UnpackingExpression) Name(org.sonar.plugins.python.api.tree.Name) Map(java.util.Map) QualifiedExpression(org.sonar.plugins.python.api.tree.QualifiedExpression) TokenLocation(org.sonar.python.TokenLocation) PythonPunctuator(org.sonar.python.api.PythonPunctuator) Expression(org.sonar.plugins.python.api.tree.Expression) Path(java.nio.file.Path) Nullable(javax.annotation.Nullable) SymbolUtils(org.sonar.python.semantic.SymbolUtils) KeyValuePair(org.sonar.plugins.python.api.tree.KeyValuePair) SymbolUtils.pathOf(org.sonar.python.semantic.SymbolUtils.pathOf) Set(java.util.Set) StringLiteral(org.sonar.plugins.python.api.tree.StringLiteral) SubscriptionContext(org.sonar.plugins.python.api.SubscriptionContext) ClassSymbol(org.sonar.plugins.python.api.symbols.ClassSymbol) Collectors(java.util.stream.Collectors) FunctionSymbolImpl(org.sonar.python.semantic.FunctionSymbolImpl) List(java.util.List) CallExpression(org.sonar.plugins.python.api.tree.CallExpression) Argument(org.sonar.plugins.python.api.tree.Argument) Optional(java.util.Optional) Tree(org.sonar.plugins.python.api.tree.Tree) Rule(org.sonar.check.Rule) LocationInFile(org.sonar.plugins.python.api.LocationInFile) DictionaryLiteral(org.sonar.plugins.python.api.tree.DictionaryLiteral) Collections(java.util.Collections) FunctionSymbol(org.sonar.plugins.python.api.symbols.FunctionSymbol) Symbol(org.sonar.plugins.python.api.symbols.Symbol) FunctionSymbol(org.sonar.plugins.python.api.symbols.FunctionSymbol) ClassSymbol(org.sonar.plugins.python.api.symbols.ClassSymbol) FunctionSymbol(org.sonar.plugins.python.api.symbols.FunctionSymbol) Symbol(org.sonar.plugins.python.api.symbols.Symbol) CallExpression(org.sonar.plugins.python.api.tree.CallExpression)

Aggregations

QualifiedExpression (org.sonar.plugins.python.api.tree.QualifiedExpression)36 Test (org.junit.Test)22 FileInput (org.sonar.plugins.python.api.tree.FileInput)15 CallExpression (org.sonar.plugins.python.api.tree.CallExpression)13 Name (org.sonar.plugins.python.api.tree.Name)13 Symbol (org.sonar.plugins.python.api.symbols.Symbol)10 Expression (org.sonar.plugins.python.api.tree.Expression)10 RuleTest (org.sonar.python.parser.RuleTest)8 Token (org.sonar.plugins.python.api.tree.Token)6 Tree (org.sonar.plugins.python.api.tree.Tree)6 List (java.util.List)5 Optional (java.util.Optional)5 BinaryExpression (org.sonar.plugins.python.api.tree.BinaryExpression)5 HashMap (java.util.HashMap)4 ClassSymbol (org.sonar.plugins.python.api.symbols.ClassSymbol)4 FunctionSymbol (org.sonar.plugins.python.api.symbols.FunctionSymbol)4 AliasedName (org.sonar.plugins.python.api.tree.AliasedName)4 ImportName (org.sonar.plugins.python.api.tree.ImportName)4 RegularArgument (org.sonar.plugins.python.api.tree.RegularArgument)4 StringLiteral (org.sonar.plugins.python.api.tree.StringLiteral)4