@Override public void visitToken(DetailAST ast) { if (!isInContext(ast, ALLOWED_ASSIGNMENT_CONTEXT) && !isInNoBraceControlStatement(ast) && !isInWhileIdiom(ast)) { log(ast, MSG_KEY); } }
/** * Tests whether the given AST is used in the "assignment in while" idiom. * <pre> * String line; * while ((line = bufferedReader.readLine()) != null) { * // process the line * } * </pre> * Assignment inside a condition is not a problem here, as the assignment is surrounded by an * extra pair of parentheses. The comparison is {@code != null} and there is no chance that * intention was to write {@code line == reader.readLine()}. * * @param ast assignment AST * @return whether the context of the assignment AST indicates the idiom */ private static boolean isInWhileIdiom(DetailAST ast) { boolean result = false; if (isComparison(ast.getParent())) { result = isInContext( ast.getParent(), ALLOWED_ASSIGNMENT_IN_COMPARISON_CONTEXT); } return result; }
if (isInContext(ast, CONTROL_CONTEXT)) { final DetailAST expr = ast.getParent(); final AST exprNext = expr.getNextSibling();
@Override public void visitToken(DetailAST ast) { if (!isInContext(ast, ALLOWED_ASSIGNMENT_CONTEXT) && !isInNoBraceControlStatement(ast) && !isInWhileIdiom(ast)) { log(ast, MSG_KEY); } }
/** * Tests whether the given AST is used in the "assignment in while" idiom. * <pre> * String line; * while ((line = bufferedReader.readLine()) != null) { * // process the line * } * </pre> * Assignment inside a condition is not a problem here, as the assignment is surrounded by an * extra pair of parentheses. The comparison is {@code != null} and there is no chance that * intention was to write {@code line == reader.readLine()}. * * @param ast assignment AST * @return whether the context of the assignment AST indicates the idiom */ private static boolean isInWhileIdiom(DetailAST ast) { boolean result = false; if (isComparison(ast.getParent())) { result = isInContext( ast.getParent(), ALLOWED_ASSIGNMENT_IN_COMPARISON_CONTEXT); } return result; }
if (isInContext(ast, CONTROL_CONTEXT)) { final DetailAST expr = ast.getParent(); final AST exprNext = expr.getNextSibling();