Search in sources :

Example 1 with ExceptClause

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

the class UnreachableExceptCheck method initialize.

@Override
public void initialize(Context context) {
    context.registerSyntaxNodeConsumer(Tree.Kind.TRY_STMT, ctx -> {
        TryStatement tryStatement = (TryStatement) ctx.syntaxNode();
        Map<String, Expression> caughtTypes = new HashMap<>();
        for (ExceptClause exceptClause : tryStatement.exceptClauses()) {
            handleExceptClause(ctx, caughtTypes, exceptClause);
        }
    });
}
Also used : TryStatement(org.sonar.plugins.python.api.tree.TryStatement) Expression(org.sonar.plugins.python.api.tree.Expression) HashMap(java.util.HashMap) ExceptClause(org.sonar.plugins.python.api.tree.ExceptClause)

Example 2 with ExceptClause

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

the class CaughtExceptionsCheck method initialize.

@Override
public void initialize(Context context) {
    context.registerSyntaxNodeConsumer(EXCEPT_CLAUSE, ctx -> {
        Expression exception = ((ExceptClause) ctx.syntaxNode()).exception();
        if (exception == null) {
            return;
        }
        TreeUtils.flattenTuples(exception).forEach(expression -> {
            if (!canBeOrExtendBaseException(expression.type()) || ((expression instanceof HasSymbol) && !inheritsFromBaseException(((HasSymbol) expression).symbol()))) {
                ctx.addIssue(expression, MESSAGE);
            }
        });
    });
}
Also used : Expression(org.sonar.plugins.python.api.tree.Expression) ExceptClause(org.sonar.plugins.python.api.tree.ExceptClause) HasSymbol(org.sonar.plugins.python.api.tree.HasSymbol)

Example 3 with ExceptClause

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

the class IgnoredSystemExitCheck method initialize.

@Override
public void initialize(Context context) {
    context.registerSyntaxNodeConsumer(Tree.Kind.TRY_STMT, ctx -> {
        TryStatement tryStatement = (TryStatement) ctx.syntaxNode();
        boolean isSystemExitHandled = false;
        for (ExceptClause exceptClause : tryStatement.exceptClauses()) {
            Expression exceptionExpr = exceptClause.exception();
            if (exceptionExpr == null) {
                handlePossibleBareException(ctx, exceptClause, isSystemExitHandled);
                break;
            }
            // Find the possible exception instance name
            Expression exceptionInstance = exceptClause.exceptionInstance();
            Symbol exceptionInstanceSymbol = findExceptionInstanceSymbol(exceptionInstance);
            List<Expression> caughtExceptions = TreeUtils.flattenTuples(exceptionExpr).collect(Collectors.toList());
            for (Expression caughtException : caughtExceptions) {
                isSystemExitHandled |= handleCaughtException(ctx, caughtException, exceptionInstanceSymbol, exceptClause.body(), isSystemExitHandled);
            }
        }
    });
}
Also used : TryStatement(org.sonar.plugins.python.api.tree.TryStatement) CallExpression(org.sonar.plugins.python.api.tree.CallExpression) Expression(org.sonar.plugins.python.api.tree.Expression) HasSymbol(org.sonar.plugins.python.api.tree.HasSymbol) Symbol(org.sonar.plugins.python.api.symbols.Symbol) ExceptClause(org.sonar.plugins.python.api.tree.ExceptClause)

Example 4 with ExceptClause

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

the class UselessParenthesisAfterKeywordCheck method initialize.

@Override
public void initialize(Context context) {
    context.registerSyntaxNodeConsumer(Tree.Kind.ASSERT_STMT, ctx -> checkExpr(((AssertStatement) ctx.syntaxNode()).condition(), ctx, "assert"));
    context.registerSyntaxNodeConsumer(Tree.Kind.DEL_STMT, ctx -> checkExpr(((DelStatement) ctx.syntaxNode()).expressions().get(0), ctx, "del"));
    context.registerSyntaxNodeConsumer(Tree.Kind.IF_STMT, ctx -> {
        IfStatement ifStmt = (IfStatement) ctx.syntaxNode();
        checkExpr(ifStmt.condition(), ctx, ifStmt.keyword().value());
    });
    context.registerSyntaxNodeConsumer(Tree.Kind.WHILE_STMT, ctx -> {
        WhileStatement whileStmt = (WhileStatement) ctx.syntaxNode();
        checkExpr(whileStmt.condition(), ctx, whileStmt.whileKeyword().value());
    });
    context.registerSyntaxNodeConsumer(Tree.Kind.FOR_STMT, ctx -> handleForStatement(ctx, (ForStatement) ctx.syntaxNode()));
    context.registerSyntaxNodeConsumer(Tree.Kind.RAISE_STMT, ctx -> handleRaiseStatement(ctx, (RaiseStatement) ctx.syntaxNode()));
    context.registerSyntaxNodeConsumer(Tree.Kind.RETURN_STMT, ctx -> handleReturnStatement(ctx, (ReturnStatement) ctx.syntaxNode()));
    context.registerSyntaxNodeConsumer(Tree.Kind.YIELD_EXPR, ctx -> handleYieldExpression(ctx, (YieldExpression) ctx.syntaxNode()));
    context.registerSyntaxNodeConsumer(Tree.Kind.EXCEPT_CLAUSE, ctx -> {
        Expression exception = ((ExceptClause) ctx.syntaxNode()).exception();
        if (exception != null) {
            checkExprExcludeTuples(exception, ctx, "except");
        }
    });
    context.registerSyntaxNodeConsumer(Tree.Kind.NOT, ctx -> handleNotOperator(ctx, (UnaryExpression) ctx.syntaxNode()));
}
Also used : IfStatement(org.sonar.plugins.python.api.tree.IfStatement) YieldExpression(org.sonar.plugins.python.api.tree.YieldExpression) BinaryExpression(org.sonar.plugins.python.api.tree.BinaryExpression) ParenthesizedExpression(org.sonar.plugins.python.api.tree.ParenthesizedExpression) UnaryExpression(org.sonar.plugins.python.api.tree.UnaryExpression) Expression(org.sonar.plugins.python.api.tree.Expression) YieldExpression(org.sonar.plugins.python.api.tree.YieldExpression) AssertStatement(org.sonar.plugins.python.api.tree.AssertStatement) ReturnStatement(org.sonar.plugins.python.api.tree.ReturnStatement) UnaryExpression(org.sonar.plugins.python.api.tree.UnaryExpression) WhileStatement(org.sonar.plugins.python.api.tree.WhileStatement) ForStatement(org.sonar.plugins.python.api.tree.ForStatement) ExceptClause(org.sonar.plugins.python.api.tree.ExceptClause) RaiseStatement(org.sonar.plugins.python.api.tree.RaiseStatement)

Example 5 with ExceptClause

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

the class ControlFlowGraphBuilder method exceptClauses.

private PythonCfgBlock exceptClauses(TryStatement tryStatement, PythonCfgBlock finallyOrAfterTryBlock, @Nullable PythonCfgBlock finallyBlock) {
    PythonCfgBlock falseSuccessor = finallyBlock == null ? exceptionTargets.peek() : finallyBlock;
    List<ExceptClause> exceptClauses = tryStatement.exceptClauses();
    for (int i = exceptClauses.size() - 1; i >= 0; i--) {
        ExceptClause exceptClause = exceptClauses.get(i);
        PythonCfgBlock exceptBlock = build(exceptClause.body().statements(), createSimpleBlock(finallyOrAfterTryBlock));
        PythonCfgBlock exceptCondition = createBranchingBlock(exceptClause, exceptBlock, falseSuccessor);
        Expression exceptionInstance = exceptClause.exceptionInstance();
        if (exceptionInstance != null) {
            exceptCondition.addElement(exceptionInstance);
        }
        Expression exception = exceptClause.exception();
        if (exception != null) {
            exceptCondition.addElement(exception);
        }
        falseSuccessor = exceptCondition;
    }
    return falseSuccessor;
}
Also used : Expression(org.sonar.plugins.python.api.tree.Expression) ExceptClause(org.sonar.plugins.python.api.tree.ExceptClause)

Aggregations

ExceptClause (org.sonar.plugins.python.api.tree.ExceptClause)10 Expression (org.sonar.plugins.python.api.tree.Expression)9 TryStatement (org.sonar.plugins.python.api.tree.TryStatement)5 RaiseStatement (org.sonar.plugins.python.api.tree.RaiseStatement)3 AstNode (com.sonar.sslr.api.AstNode)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 AssertStatement (org.sonar.plugins.python.api.tree.AssertStatement)2 HasSymbol (org.sonar.plugins.python.api.tree.HasSymbol)2 Tree (org.sonar.plugins.python.api.tree.Tree)2 GenericTokenType (com.sonar.sslr.api.GenericTokenType)1 RecognitionException (com.sonar.sslr.api.RecognitionException)1 Collections (java.util.Collections)1 Objects (java.util.Objects)1 Collectors (java.util.stream.Collectors)1 CheckForNull (javax.annotation.CheckForNull)1 Nullable (javax.annotation.Nullable)1 Test (org.junit.Test)1 ClassSymbol (org.sonar.plugins.python.api.symbols.ClassSymbol)1