Search in sources :

Example 1 with StatementList

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

the class OverwrittenCollectionEntryCheck method check.

private static void check(SubscriptionContext ctx, StatementList statementList) {
    Map<CollectionKey, List<CollectionWrite>> collectionWrites = new HashMap<>();
    for (Statement statement : statementList.statements()) {
        CollectionWrite write = null;
        if (statement.is(Kind.ASSIGNMENT_STMT)) {
            AssignmentStatement assignment = (AssignmentStatement) statement;
            Expression expression = lhs(assignment);
            write = collectionWrite(assignment, expression);
        }
        if (write != null) {
            collectionWrites.computeIfAbsent(write.collectionKey, k -> new ArrayList<>()).add(write);
        } else {
            reportOverwrites(ctx, collectionWrites);
            collectionWrites.clear();
        }
    }
    reportOverwrites(ctx, collectionWrites);
}
Also used : NumericLiteral(org.sonar.plugins.python.api.tree.NumericLiteral) PythonSubscriptionCheck(org.sonar.plugins.python.api.PythonSubscriptionCheck) HasSymbol(org.sonar.plugins.python.api.tree.HasSymbol) HashMap(java.util.HashMap) AssignmentStatement(org.sonar.plugins.python.api.tree.AssignmentStatement) ArrayList(java.util.ArrayList) TreeUtils(org.sonar.python.tree.TreeUtils) Kind(org.sonar.plugins.python.api.tree.Tree.Kind) IssueLocation(org.sonar.plugins.python.api.IssueLocation) Name(org.sonar.plugins.python.api.tree.Name) Map(java.util.Map) Statement(org.sonar.plugins.python.api.tree.Statement) Expression(org.sonar.plugins.python.api.tree.Expression) Nullable(javax.annotation.Nullable) StatementList(org.sonar.plugins.python.api.tree.StatementList) SliceItem(org.sonar.plugins.python.api.tree.SliceItem) StringLiteral(org.sonar.plugins.python.api.tree.StringLiteral) Token(org.sonar.plugins.python.api.tree.Token) SubscriptionContext(org.sonar.plugins.python.api.SubscriptionContext) Collectors(java.util.stream.Collectors) UnaryExpression(org.sonar.plugins.python.api.tree.UnaryExpression) AbstractMap(java.util.AbstractMap) List(java.util.List) Stream(java.util.stream.Stream) SliceExpression(org.sonar.plugins.python.api.tree.SliceExpression) SubscriptionExpression(org.sonar.plugins.python.api.tree.SubscriptionExpression) Tree(org.sonar.plugins.python.api.tree.Tree) Rule(org.sonar.check.Rule) CheckForNull(javax.annotation.CheckForNull) Symbol(org.sonar.plugins.python.api.symbols.Symbol) HashMap(java.util.HashMap) AssignmentStatement(org.sonar.plugins.python.api.tree.AssignmentStatement) Expression(org.sonar.plugins.python.api.tree.Expression) UnaryExpression(org.sonar.plugins.python.api.tree.UnaryExpression) SliceExpression(org.sonar.plugins.python.api.tree.SliceExpression) SubscriptionExpression(org.sonar.plugins.python.api.tree.SubscriptionExpression) AssignmentStatement(org.sonar.plugins.python.api.tree.AssignmentStatement) Statement(org.sonar.plugins.python.api.tree.Statement) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) StatementList(org.sonar.plugins.python.api.tree.StatementList) List(java.util.List)

Example 2 with StatementList

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

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

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

the class DuplicatedMethodImplementationCheck method isException.

private static boolean isException(FunctionDef suspiciousMethod) {
    boolean hasDocString = suspiciousMethod.docstring() != null;
    StatementList suspiciousBody = suspiciousMethod.body();
    List<Statement> statements = suspiciousBody.statements();
    int nbActualStatements = hasDocString ? statements.size() - 1 : statements.size();
    if (nbActualStatements == 0 || isOnASingleLine(suspiciousBody, hasDocString)) {
        return true;
    }
    return nbActualStatements == 1 && statements.get(statements.size() - 1).is(Tree.Kind.RAISE_STMT);
}
Also used : Statement(org.sonar.plugins.python.api.tree.Statement) StatementList(org.sonar.plugins.python.api.tree.StatementList)

Example 5 with StatementList

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

the class EmptyNestedBlockCheck method initialize.

@Override
public void initialize(Context context) {
    context.registerSyntaxNodeConsumer(Kind.STATEMENT_LIST, ctx -> {
        StatementList statementListTree = (StatementList) ctx.syntaxNode();
        if (statementListTree.statements().stream().anyMatch(stmt -> !stmt.is(Kind.PASS_STMT))) {
            return;
        }
        Tree parent = statementListTree.parent();
        if (parent.is(Kind.FUNCDEF) || parent.is(Kind.CLASSDEF) || parent.is(Kind.EXCEPT_CLAUSE)) {
            return;
        }
        List<Token> parentTokens = TreeUtils.tokens(statementListTree.parent());
        int from = parentTokens.stream().filter(t -> t.type() == PythonTokenType.NEWLINE).findFirst().map(parentTokens::indexOf).orElseThrow(() -> new IllegalStateException(String.format("No newline token in parent of statement list at line %s", statementListTree.firstToken().line())));
        // sublist call is excluding last index and token following last token of statement list (dedent) should be included in the comment verification.
        int to = parentTokens.indexOf(statementListTree.lastToken()) + 2;
        if (!containsComment(parentTokens.subList(from, to))) {
            if (statementListTree.statements().isEmpty()) {
                ctx.addIssue(statementListTree.firstToken(), MESSAGE);
            } else {
                ctx.addIssue(statementListTree.statements().get(0), MESSAGE);
            }
        }
    });
}
Also used : Kind(org.sonar.plugins.python.api.tree.Tree.Kind) List(java.util.List) PythonSubscriptionCheck(org.sonar.plugins.python.api.PythonSubscriptionCheck) StatementList(org.sonar.plugins.python.api.tree.StatementList) PythonTokenType(org.sonar.python.api.PythonTokenType) Token(org.sonar.plugins.python.api.tree.Token) Tree(org.sonar.plugins.python.api.tree.Tree) Rule(org.sonar.check.Rule) TreeUtils(org.sonar.python.tree.TreeUtils) StatementList(org.sonar.plugins.python.api.tree.StatementList) Tree(org.sonar.plugins.python.api.tree.Tree) Token(org.sonar.plugins.python.api.tree.Token)

Aggregations

StatementList (org.sonar.plugins.python.api.tree.StatementList)21 Token (org.sonar.plugins.python.api.tree.Token)12 AstNode (com.sonar.sslr.api.AstNode)9 IfStatement (org.sonar.plugins.python.api.tree.IfStatement)6 Tree (org.sonar.plugins.python.api.tree.Tree)6 ElseClause (org.sonar.plugins.python.api.tree.ElseClause)5 FileInput (org.sonar.plugins.python.api.tree.FileInput)5 Name (org.sonar.plugins.python.api.tree.Name)5 AssignmentStatement (org.sonar.plugins.python.api.tree.AssignmentStatement)4 Expression (org.sonar.plugins.python.api.tree.Expression)4 ArrayList (java.util.ArrayList)3 Rule (org.sonar.check.Rule)3 PythonSubscriptionCheck (org.sonar.plugins.python.api.PythonSubscriptionCheck)3 AliasedName (org.sonar.plugins.python.api.tree.AliasedName)3 AssignmentExpression (org.sonar.plugins.python.api.tree.AssignmentExpression)3 ComprehensionExpression (org.sonar.plugins.python.api.tree.ComprehensionExpression)3 ConditionalExpression (org.sonar.plugins.python.api.tree.ConditionalExpression)3 Decorator (org.sonar.plugins.python.api.tree.Decorator)3 DottedName (org.sonar.plugins.python.api.tree.DottedName)3 FormattedExpression (org.sonar.plugins.python.api.tree.FormattedExpression)3