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