use of com.puppycrawl.tools.checkstyle.api.DetailAST in project checkstyle by checkstyle.
the class FallThroughCheck method checkLoop.
/**
* Checks if a given loop terminated by return, throw or,
* if allowed break, continue.
* @param ast loop to check
* @return true if loop is terminated.
*/
private boolean checkLoop(final DetailAST ast) {
final DetailAST loopBody;
if (ast.getType() == TokenTypes.LITERAL_DO) {
final DetailAST lparen = ast.findFirstToken(TokenTypes.DO_WHILE);
loopBody = lparen.getPreviousSibling();
} else {
final DetailAST rparen = ast.findFirstToken(TokenTypes.RPAREN);
loopBody = rparen.getNextSibling();
}
return isTerminated(loopBody, false, false);
}
use of com.puppycrawl.tools.checkstyle.api.DetailAST in project checkstyle by checkstyle.
the class FallThroughCheck method visitToken.
@Override
public void visitToken(DetailAST ast) {
final DetailAST nextGroup = ast.getNextSibling();
final boolean isLastGroup = nextGroup.getType() != TokenTypes.CASE_GROUP;
if (!isLastGroup || checkLastCaseGroup) {
final DetailAST slist = ast.findFirstToken(TokenTypes.SLIST);
if (slist != null && !isTerminated(slist, true, true) && !hasFallThroughComment(ast, nextGroup)) {
if (isLastGroup) {
log(ast, MSG_FALL_THROUGH_LAST);
} else {
log(nextGroup, MSG_FALL_THROUGH);
}
}
}
}
use of com.puppycrawl.tools.checkstyle.api.DetailAST in project checkstyle by checkstyle.
the class FallThroughCheck method checkSwitch.
/**
* Checks if a given switch terminated by return, throw or,
* if allowed break, continue.
* @param literalSwitchAst loop to check
* @param useContinue should we consider continue as terminator.
* @return true if switch is terminated.
*/
private boolean checkSwitch(final DetailAST literalSwitchAst, boolean useContinue) {
DetailAST caseGroup = literalSwitchAst.findFirstToken(TokenTypes.CASE_GROUP);
boolean isTerminated = caseGroup != null;
while (isTerminated && caseGroup.getType() != TokenTypes.RCURLY) {
final DetailAST caseBody = caseGroup.findFirstToken(TokenTypes.SLIST);
isTerminated = caseBody != null && isTerminated(caseBody, false, useContinue);
caseGroup = caseGroup.getNextSibling();
}
return isTerminated;
}
use of com.puppycrawl.tools.checkstyle.api.DetailAST in project checkstyle by checkstyle.
the class FinalLocalVariableCheck method updateUninitializedVariables.
/**
* Update current scope data uninitialized variable according to the previous scope data.
* @param prevScopeUnitializedVariableData variable for previous stack of uninitialized
* variables
*/
// -@cs[CyclomaticComplexity] Breaking apart will damage encapsulation.
private void updateUninitializedVariables(Deque<DetailAST> prevScopeUnitializedVariableData) {
// Check for only previous scope
for (DetailAST variable : prevScopeUnitializedVariableData) {
for (ScopeData scopeData : scopeStack) {
final FinalVariableCandidate candidate = scopeData.scope.get(variable.getText());
DetailAST storedVariable = null;
if (candidate != null) {
storedVariable = candidate.variableIdent;
}
if (storedVariable != null && isSameVariables(storedVariable, variable) && !scopeData.uninitializedVariables.contains(storedVariable)) {
scopeData.uninitializedVariables.push(variable);
}
}
}
// Check for rest of the scope
for (Deque<DetailAST> unitializedVariableData : prevScopeUninitializedVariables) {
for (DetailAST variable : unitializedVariableData) {
for (ScopeData scopeData : scopeStack) {
final FinalVariableCandidate candidate = scopeData.scope.get(variable.getText());
DetailAST storedVariable = null;
if (candidate != null) {
storedVariable = candidate.variableIdent;
}
if (storedVariable != null && isSameVariables(storedVariable, variable) && !scopeData.uninitializedVariables.contains(storedVariable)) {
scopeData.uninitializedVariables.push(variable);
}
}
}
}
}
use of com.puppycrawl.tools.checkstyle.api.DetailAST in project checkstyle by checkstyle.
the class FinalLocalVariableCheck method isSameVariables.
/**
* Check if both the Variables are same.
* @param ast1 Variable to compare
* @param ast2 Variable to compare
* @return true if both the variables are same, otherwise false
*/
private static boolean isSameVariables(DetailAST ast1, DetailAST ast2) {
final DetailAST classOrMethodOfAst1 = findFirstUpperNamedBlock(ast1);
final DetailAST classOrMethodOfAst2 = findFirstUpperNamedBlock(ast2);
return classOrMethodOfAst1 == classOrMethodOfAst2;
}
Aggregations