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