private static boolean isTrivialCase(List<StatementTree> body) { return body.size() == 1 || (body.size() == 2 && body.get(1).is(Tree.Kind.BREAK_STATEMENT)); }
void visit(StatementTree node) { referenceSymbol = false; hasBreakingStatement = false; node.accept(this); }
private boolean isSameLine(IfStatementTree ifStmt) { StatementTree thenStmt = ifStmt.thenStatement(); if (thenStmt.is(Tree.Kind.BLOCK)) { return false; } return ifStmt.firstToken().line() == thenStmt.firstToken().line(); }
@Override public void visitNode(Tree tree) { IfStatementTree ifStatementTree = (IfStatementTree) tree; SyntaxToken lastToken = ifStatementTree.thenStatement().lastToken(); if (ifStatementTree.elseKeyword() == null) { if(previousToken != null && isOnSameLineAsPreviousIf(ifStatementTree)) { reportIssue(ifStatementTree.ifKeyword(), "Move this \"if\" to a new line or add the missing \"else\".", Collections.singletonList(new JavaFileScannerContext.Location("", previousToken)), null); } previousToken = lastToken; } }
private void checkForReport(StatementTree statement, Tree startTree, Tree endTree, String name) { if (!(statement.is(Tree.Kind.BLOCK) || statement.firstToken().column() > startTree.firstToken().column())) { context.reportIssue(this, startTree, endTree, "Use indentation to denote the code conditionally executed by this \"" + name + "\".", Collections.singletonList(new JavaFileScannerContext.Location("", statement)), null); } } }
@Override public void visitNode(Tree tree) { IfStatementTree ifStatementTree = (IfStatementTree) tree; SyntaxToken lastToken = ifStatementTree.thenStatement().lastToken(); if (ifStatementTree.elseKeyword() == null) { if(previousToken != null && isOnSameLineAsPreviousIf(ifStatementTree)) { reportIssue(ifStatementTree.ifKeyword(), "Move this \"if\" to a new line or add the missing \"else\".", Collections.singletonList(new JavaFileScannerContext.Location("", previousToken)), null); } previousToken = lastToken; } }
private static boolean hasBodySingleIfStatement(StatementTree thenStatement) { if (thenStatement.is(Tree.Kind.BLOCK)) { // thenStatement has curly braces. Let's see what's inside... BlockTree block = (BlockTree) thenStatement; return block.body().size() == 1 && block.body().get(0).is(Tree.Kind.IF_STATEMENT); } else if (thenStatement.is(Tree.Kind.IF_STATEMENT)) { // no curlys on thenStatement; it's a bare if statement return true; } return false; } }
private void checkForReport(StatementTree statement, Tree startTree, Tree endTree, String name) { if (!(statement.is(Tree.Kind.BLOCK) || statement.firstToken().column() > startTree.firstToken().column())) { context.reportIssue(this, startTree, endTree, "Use indentation to denote the code conditionally executed by this \"" + name + "\".", Collections.singletonList(new JavaFileScannerContext.Location("", statement)), null); } } }
void visit(StatementTree node) { referenceSymbol = false; hasBreakingStatement = false; node.accept(this); }
private static boolean hasBodySingleIfStatement(StatementTree thenStatement) { if (thenStatement.is(Tree.Kind.BLOCK)) { // thenStatement has curly braces. Let's see what's inside... BlockTree block = (BlockTree) thenStatement; return block.body().size() == 1 && block.body().get(0).is(Tree.Kind.IF_STATEMENT); } else if (thenStatement.is(Tree.Kind.IF_STATEMENT)) { // no curlys on thenStatement; it's a bare if statement return true; } return false; } }
private void check(StatementTree current, StatementTree previous) { StatementTree block = null; boolean condition = false; if (previous.is(Tree.Kind.FOR_EACH_STATEMENT)) { block = ((ForEachStatement) previous).statement(); } else if (previous.is(Tree.Kind.FOR_STATEMENT)) { block = ((ForStatementTree) previous).statement(); } else if (previous.is(Tree.Kind.WHILE_STATEMENT)) { block = ((WhileStatementTree) previous).statement(); } else if (previous.is(Tree.Kind.IF_STATEMENT)) { block = getIfStatementLastBlock(previous); condition = true; } if (block != null && !block.is(Tree.Kind.BLOCK)) { SyntaxToken previousToken = block.firstToken(); int previousColumn = previousToken.column(); int previousLine = previousToken.line(); SyntaxToken currentToken = current.firstToken(); int currentColumn = currentToken.column(); int currentLine = currentToken.line(); if ((previousColumn == currentColumn && previousLine + 1 == currentLine) || (previousLine == previous.firstToken().line() && previous.firstToken().column() < currentColumn)) { int lines = 1 + currentLine - previousLine; context.reportIssue(this, current, getMessage(condition, lines)); } } }
private static boolean thenStmtInitializeField(StatementTree statementTree, Symbol field) { AssignmentVisitor visitor = new AssignmentVisitor(field); statementTree.accept(visitor); return visitor.assignmentToField; }
private static boolean hasBodySingleIfStatement(StatementTree thenStatement) { if (thenStatement.is(Tree.Kind.BLOCK)) { // thenStatement has curly braces. Let's see what's inside... BlockTree block = (BlockTree) thenStatement; return block.body().size() == 1 && block.body().get(0).is(Tree.Kind.IF_STATEMENT); } else if (thenStatement.is(Tree.Kind.IF_STATEMENT)) { // no curlys on thenStatement; it's a bare if statement return true; } return false; } }
private void check(StatementTree current, StatementTree previous) { StatementTree block = null; boolean condition = false; if (previous.is(Tree.Kind.FOR_EACH_STATEMENT)) { block = ((ForEachStatement) previous).statement(); } else if (previous.is(Tree.Kind.FOR_STATEMENT)) { block = ((ForStatementTree) previous).statement(); } else if (previous.is(Tree.Kind.WHILE_STATEMENT)) { block = ((WhileStatementTree) previous).statement(); } else if (previous.is(Tree.Kind.IF_STATEMENT)) { block = getIfStatementLastBlock(previous); condition = true; } if (block != null && !block.is(Tree.Kind.BLOCK)) { SyntaxToken previousToken = block.firstToken(); int previousColumn = previousToken.column(); int previousLine = previousToken.line(); SyntaxToken currentToken = current.firstToken(); int currentColumn = currentToken.column(); int currentLine = currentToken.line(); if ((previousColumn == currentColumn && previousLine + 1 == currentLine) || (previousLine == previous.firstToken().line() && previous.firstToken().column() < currentColumn)) { int lines = 1 + currentLine - previousLine; context.reportIssue(this, current, getMessage(condition, lines)); } } }
private static boolean thenStmtInitializeField(StatementTree statementTree, Symbol field) { AssignmentVisitor visitor = new AssignmentVisitor(field); statementTree.accept(visitor); return visitor.assignmentToField; }
public void addVariables(List<StatementTree> statementTrees) { for (StatementTree statementTree : statementTrees) { if (statementTree.is(Tree.Kind.VARIABLE)) { addVariable((VariableTree) statementTree); } } }
private static boolean forBodyUpdatesLoopIdentifier(ForStatementTree forStatement, IdentifierTree loopIdentifier) { LoopVariableAssignmentVisitor visitor = new LoopVariableAssignmentVisitor(loopIdentifier); forStatement.statement().accept(visitor); return visitor.foundAssignment; }
@CheckForNull private static ExpressionTree expressionFromSingleStatementBlock(List<StatementTree> body) { if (body.size() == 1) { StatementTree singleStatement = body.get(0); if (singleStatement.is(Tree.Kind.EXPRESSION_STATEMENT)) { return ((ExpressionStatementTree) singleStatement).expression(); } } return null; }
private static Map<Symbol, Tree> singleUpdatesInBody(StatementTree statement, Collection<Symbol> conditionsNotUpdated) { UpdatesInBodyVisitor updatedInBodyVisitor = new UpdatesInBodyVisitor(conditionsNotUpdated); statement.accept(updatedInBodyVisitor); // only report if there is symbols which are updated only once, as multiple update cannot easily be moved to updates return updatedOnlyOnceWithUnaryExpression(updatedInBodyVisitor.updates, statement); }
private void checkStatement(SyntaxToken reportToken, StatementTree statement) { if (!statement.is(Tree.Kind.BLOCK)) { reportIssue(reportToken, "Missing curly brace."); } } }