private static DefaultClauseTree getDefaultClause(SwitchStatementTree switchStmt) { for (SwitchClauseTree clause : switchStmt.cases()) { if (clause.is(Kind.DEFAULT_CLAUSE)) { return (DefaultClauseTree) clause; } } return null; }
private static boolean hasDefaultCase(SwitchStatementTree switchStmt) { for (SwitchClauseTree clause : switchStmt.cases()) { if (clause.is(Kind.DEFAULT_CLAUSE)) { return true; } } return false; }
private static boolean hasCommentFallThroughIntentional(SwitchClauseTree caseClause, SwitchStatementTree switchStatement) { List<SwitchClauseTree> cases = switchStatement.cases(); for (int i = 0; i < cases.size() - 1; i++) { if (cases.get(i).equals(caseClause)) { return !cases.get(i + 1).firstToken().trivias().isEmpty(); } } return false; }
@Override public void visitSwitchStatement(SwitchStatementTree tree) { for (int i = 0; i < tree.cases().size(); i++) { SwitchClauseTree caseTree = tree.cases().get(i); // FIXME martin: Don't check duplication for case with fall through on the next case. if (caseTree.statements().isEmpty() || isCaseEndingWithoutJumpStmt(caseTree)) { continue; } compareWithNextCases(tree, i, caseTree); } }
@Override public void visitSwitchStatement(SwitchStatementTree tree) { for (int i = 0; i < tree.cases().size(); i++) { for (int j = i + 1; j < tree.cases().size(); j++) { ExpressionTree condition = getCondition(tree.cases().get(i)); ExpressionTree conditionToCompare = getCondition(tree.cases().get(j)); if (SyntacticEquivalence.areEquivalent(condition, conditionToCompare)) { getContext().addIssue(this, conditionToCompare, "This case duplicates the one on line " + ((AstNode) condition).getTokenLine() + "."); } } } }
@Override public void visitSwitchStatement(SwitchStatementTree tree) { if (tree.cases().size() < 3) { getContext().addIssue(this, tree, "Replace this \"switch\" statement with \"if\" statements to increase readability."); } super.visitSwitchStatement(tree); }
@Override public void visitSwitchStatement(SwitchStatementTree tree) { boolean hasFallthrough = false; boolean hasDefaultCase = false; List<Tree> branches = new ArrayList<>(); for (int i = 0; i < tree.cases().size(); i++) { SwitchClauseTree caseTree = tree.cases().get(i); boolean isLast = (i == tree.cases().size() - 1); if (caseTree.is(Kind.DEFAULT_CLAUSE)) { hasDefaultCase = true; } if (!caseTree.statements().isEmpty() || isLast) { branches.add(caseTree); } if (!endsWithJump(caseTree) && !isLast) { hasFallthrough = true; } } if (!hasFallthrough && hasDefaultCase && allBranchesEquivalent(branches)) { allBranchesDuplicated(tree); } else { checkDuplicatedBranches(branches); } super.visitSwitchStatement(tree); }
@Override public void visitSwitchStatement(SwitchStatementTree tree) { List<SwitchClauseTree> cases = new LinkedList<>(tree.cases()); cases.remove(cases.size() - 1); for (SwitchClauseTree switchClauseTree : cases){ List<StatementTree> statements = switchClauseTree.statements(); if (!statements.isEmpty() && !Iterables.getLast(statements).is(Kind.BREAK_STATEMENT, Kind.RETURN_STATEMENT, Kind.THROW_STATEMENT)) { getContext().addIssue(this, switchClauseTree, "Last statement in this switch-clause should be an unconditional break."); } } super.visitSwitchStatement(tree); } }
@Override public void visitSwitchStatement(SwitchStatementTree tree) { scan(tree.expression()); scan(tree.cases()); }
private void compareWithNextCases(SwitchStatementTree tree, int indexCaseReference, SwitchClauseTree caseTree) { for (int j = indexCaseReference + 1; j < tree.cases().size(); j++) { SwitchClauseTree caseTreeToCompare = tree.cases().get(j); // FIXME martin: Don't check duplication for case with fall through on the next case. if (caseTreeToCompare.statements().isEmpty() || isCaseEndingWithoutJumpStmt(caseTreeToCompare)) { continue; } // Remove the jump statement if comparing to default case List<StatementTree> caseStatements = caseTreeToCompare.is(Kind.DEFAULT_CLAUSE) ? caseTree.statements().subList(0, caseTree.statements().size() - 1) : caseTree.statements(); if (SyntacticEquivalence.areEquivalent(caseStatements, caseTreeToCompare.statements())) { getContext().addIssue(this, caseTreeToCompare, "Either merge this case with the identical one on line \"" + ((AstNode) caseTree).getTokenLine() + "\" or change one of the implementations."); // break the inner loop break; } } }
@Override public void visitSwitchStatement(SwitchStatementTree tree) { DefaultClauseTree defaultClause = getDefaultClause(tree); if (defaultClause == null) { addIssue(tree.switchKeyword(), ADD_DEFAULT_MESSAGE); } else if (!Iterables.getLast(tree.cases()).is(Kind.DEFAULT_CLAUSE)) { addIssue(defaultClause.keyword(), MOVE_DEFAULT_MESSAGE); } super.visitSwitchStatement(tree); }
@Override public void visitSwitchStatement(SwitchStatementTree tree) { if (!hasDefaultCase(tree)) { getContext().addIssue(this, tree, "Avoid switch statement without a \"default\" clause."); } else if (!Iterables.getLast(tree.cases()).is(Kind.DEFAULT_CLAUSE)) { getContext().addIssue(this, tree, "\"default\" clause should be the last one."); } super.visitSwitchStatement(tree); }
CaseClauseTree lastCaseClause = null; for (CaseClauseTree caseClause : Iterables.filter(tree.cases(), CaseClauseTree.class)) { addCaseExpression(caseExpressions, caseClause.expression()); lastCaseClause = caseClause; SwitchClauseTree previousClauseWithStatement = null; for (SwitchClauseTree switchClause : tree.cases()) {