Search in sources :

Example 1 with IfStatement

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

the class SameBranchCheck method getIfBranches.

private List<StatementList> getIfBranches(IfStatement ifStmt) {
    List<StatementList> branches = new ArrayList<>();
    branches.add(ifStmt.body());
    branches.addAll(ifStmt.elifBranches().stream().map(IfStatement::body).collect(Collectors.toList()));
    ElseClause elseClause = ifStmt.elseBranch();
    if (elseClause != null) {
        branches.add(elseClause.body());
        lookForElseIfs(branches, elseClause);
    }
    return branches;
}
Also used : IfStatement(org.sonar.plugins.python.api.tree.IfStatement) ElseClause(org.sonar.plugins.python.api.tree.ElseClause) StatementList(org.sonar.plugins.python.api.tree.StatementList) ArrayList(java.util.ArrayList)

Example 2 with IfStatement

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

the class SameBranchCheck method initialize.

@Override
public void initialize(Context context) {
    context.registerSyntaxNodeConsumer(Tree.Kind.FILE_INPUT, ctx -> ignoreList = new ArrayList<>());
    context.registerSyntaxNodeConsumer(Tree.Kind.IF_STMT, ctx -> {
        IfStatement ifStmt = (IfStatement) ctx.syntaxNode();
        if (ignoreList.contains(ifStmt)) {
            return;
        }
        boolean hasElseClause = ifStmt.elseBranch() != null;
        // In this case, S3923 will raise a bug
        if (hasElseClause && allIdenticalBranches(ifStmt)) {
            return;
        }
        List<StatementList> branches = getIfBranches(ifStmt);
        findSameBranches(branches, ctx);
    });
}
Also used : IfStatement(org.sonar.plugins.python.api.tree.IfStatement) StatementList(org.sonar.plugins.python.api.tree.StatementList) ArrayList(java.util.ArrayList)

Example 3 with IfStatement

use of org.sonar.plugins.python.api.tree.IfStatement 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 4 with IfStatement

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

the class SameConditionCheck method getConditionsToCompare.

private List<Expression> getConditionsToCompare(IfStatement ifStatement) {
    List<Expression> conditions = new ArrayList<>();
    conditions.add(ifStatement.condition());
    conditions.addAll(ifStatement.elifBranches().stream().map(IfStatement::condition).collect(Collectors.toList()));
    ElseClause elseClause = ifStatement.elseBranch();
    if (elseClause != null) {
        lookForElseIfs(conditions, elseClause);
    }
    return conditions;
}
Also used : IfStatement(org.sonar.plugins.python.api.tree.IfStatement) ElseClause(org.sonar.plugins.python.api.tree.ElseClause) Expression(org.sonar.plugins.python.api.tree.Expression) ArrayList(java.util.ArrayList)

Example 5 with IfStatement

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

the class SameConditionCheck method initialize.

@Override
public void initialize(Context context) {
    context.registerSyntaxNodeConsumer(Tree.Kind.FILE_INPUT, ctx -> this.ignoreList = new ArrayList<>());
    context.registerSyntaxNodeConsumer(Tree.Kind.IF_STMT, ctx -> {
        IfStatement ifStatement = (IfStatement) ctx.syntaxNode();
        if (ignoreList.contains(ifStatement)) {
            return;
        }
        List<Expression> conditions = getConditionsToCompare(ifStatement);
        findSameConditions(conditions, ctx);
    });
}
Also used : IfStatement(org.sonar.plugins.python.api.tree.IfStatement) Expression(org.sonar.plugins.python.api.tree.Expression) ArrayList(java.util.ArrayList)

Aggregations

IfStatement (org.sonar.plugins.python.api.tree.IfStatement)18 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)5 Token (org.sonar.plugins.python.api.tree.Token)5 ElseClause (org.sonar.plugins.python.api.tree.ElseClause)4 Expression (org.sonar.plugins.python.api.tree.Expression)4 StatementList (org.sonar.plugins.python.api.tree.StatementList)4 RuleTest (org.sonar.python.parser.RuleTest)4 AstNode (com.sonar.sslr.api.AstNode)3 FileInput (org.sonar.plugins.python.api.tree.FileInput)3 BinaryExpression (org.sonar.plugins.python.api.tree.BinaryExpression)2 ClassDef (org.sonar.plugins.python.api.tree.ClassDef)2 ForStatement (org.sonar.plugins.python.api.tree.ForStatement)2 ParenthesizedExpression (org.sonar.plugins.python.api.tree.ParenthesizedExpression)2 UnaryExpression (org.sonar.plugins.python.api.tree.UnaryExpression)2 WhileStatement (org.sonar.plugins.python.api.tree.WhileStatement)2 YieldExpression (org.sonar.plugins.python.api.tree.YieldExpression)2 RecognitionException (com.sonar.sslr.api.RecognitionException)1 AliasedName (org.sonar.plugins.python.api.tree.AliasedName)1 AssertStatement (org.sonar.plugins.python.api.tree.AssertStatement)1