public class InnerAssignmentCheck extends AbstractCheck
Checks for assignments in subexpressions, such as in
String s = Integer.toString(i = 2);
.
Rationale: With the exception of for
iterators, all assignments
should occur in their own top-level statement to increase readability.
With inner assignments like the above it is difficult to see all places
where a variable is set.
Modifier and Type | Field and Description |
---|---|
private static int[][] |
ALLOWED_ASSIGNMENT_CONTEXT
List of allowed AST types from an assignment AST node
towards the root.
|
private static int[][] |
ALLOWED_ASSIGNMENT_IN_COMPARISON_CONTEXT
List of allowed AST types from a comparison node (above an assignment)
towards the root.
|
private static int[] |
COMPARISON_TYPES
The token types that identify comparison operators.
|
private static int[][] |
CONTROL_CONTEXT
List of allowed AST types from an assignment AST node
towards the root.
|
static java.lang.String |
MSG_KEY
A key is pointing to the warning message text in "messages.properties"
file.
|
Constructor and Description |
---|
InnerAssignmentCheck() |
Modifier and Type | Method and Description |
---|---|
int[] |
getAcceptableTokens()
The configurable token set.
|
int[] |
getDefaultTokens()
Returns the default token a check is interested in.
|
int[] |
getRequiredTokens()
The tokens that this check must be registered for.
|
private static boolean |
isComparison(DetailAST ast)
Checks if an AST is a comparison operator.
|
private static boolean |
isInContext(DetailAST ast,
int[]... contextSet)
Tests whether the provided AST is in
one of the given contexts.
|
private static boolean |
isInNoBraceControlStatement(DetailAST ast)
Determines if ast is in the body of a flow control statement without
braces.
|
private static boolean |
isInWhileIdiom(DetailAST ast)
Tests whether the given AST is used in the "assignment in while" idiom.
|
void |
visitToken(DetailAST ast)
Called to process a token.
|
beginTree, destroy, finishTree, getClassLoader, getFileContents, getLine, getLines, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, setClassLoader, setFileContents, setMessages, setTabWidth, setTokens
getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, log, setId, setSeverity
configure, contextualize, finishLocalSetup, getConfiguration, setupChild
public static final java.lang.String MSG_KEY
private static final int[][] ALLOWED_ASSIGNMENT_CONTEXT
private static final int[][] CONTROL_CONTEXT
private static final int[][] ALLOWED_ASSIGNMENT_IN_COMPARISON_CONTEXT
private static final int[] COMPARISON_TYPES
public int[] getDefaultTokens()
AbstractCheck
getDefaultTokens
in class AbstractCheck
TokenTypes
public int[] getAcceptableTokens()
AbstractCheck
getAcceptableTokens
in class AbstractCheck
TokenTypes
public int[] getRequiredTokens()
AbstractCheck
getRequiredTokens
in class AbstractCheck
TokenTypes
public void visitToken(DetailAST ast)
AbstractCheck
visitToken
in class AbstractCheck
ast
- the token to processprivate static boolean isInNoBraceControlStatement(DetailAST ast)
if (y < 0) x = y;
This leads to the following AST structure:
LITERAL_IF LPAREN EXPR // test RPAREN EXPR // body SEMI
We need to ensure that ast is in the body and not in the test.
ast
- an assignment operator ASTprivate static boolean isInWhileIdiom(DetailAST ast)
String line; while ((line = bufferedReader.readLine()) != null) { // process the line }Assignment inside a condition is not a problem here, as the assignment is surrounded by an extra pair of parentheses. The comparison is
!= null
and there is no chance that
intention was to write line == reader.readLine()
.ast
- assignment ASTprivate static boolean isComparison(DetailAST ast)
ast
- the AST to checkprivate static boolean isInContext(DetailAST ast, int[]... contextSet)
ast
- the AST from which to start walking towards rootcontextSet
- the contexts to test against.