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