public class UnnecessaryParenthesesCheck extends Check
Checks if unnecessary parentheses are used in a statement or expression. The check will flag the following with warnings:
return (x); // parens around identifier return (x + 1); // parens around return value int x = (y / 2 + 1); // parens around assignment rhs for (int i = (0); i < 10; i++) { // parens around literal t -= (z + 1); // parens around assignment rhs
The check is not "type aware", that is to say, it can't tell if parentheses are unnecessary based on the types in an expression. It also doesn't know about operator precedence and associativity; therefore it won't catch something like
int x = (a + b) + c;
In the above case, given that a, b, and c are
all int
variables, the parentheses around a + b
are not needed.
Modifier and Type | Field and Description |
---|---|
static String |
MSG_ASSIGN
A key is pointing to the warning message text in "messages.properties"
file.
|
static String |
MSG_EXPR
A key is pointing to the warning message text in "messages.properties"
file.
|
static String |
MSG_IDENT
A key is pointing to the warning message text in "messages.properties"
file.
|
static String |
MSG_LITERAL
A key is pointing to the warning message text in "messages.properties"
file.
|
static String |
MSG_RETURN
A key is pointing to the warning message text in "messages.properties"
file.
|
static String |
MSG_STRING
A key is pointing to the warning message text in "messages.properties"
file.
|
Constructor and Description |
---|
UnnecessaryParenthesesCheck() |
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.
|
void |
leaveToken(DetailAST ast)
Called after all the child nodes have been process.
|
void |
visitToken(DetailAST ast)
Called to process a token.
|
beginTree, destroy, finishTree, getClassLoader, getFileContents, getLine, getLines, getTabWidth, getTokenNames, init, isCommentNodesRequired, log, log, setClassLoader, setFileContents, setMessages, setTabWidth, setTokens
getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, log, setId, setSeverity
configure, contextualize, finishLocalSetup, getConfiguration, setupChild
public static final String MSG_IDENT
public static final String MSG_ASSIGN
public static final String MSG_EXPR
public static final String MSG_LITERAL
public static final String MSG_STRING
public static final String MSG_RETURN
public int[] getDefaultTokens()
Check
getDefaultTokens
in class Check
TokenTypes
public int[] getAcceptableTokens()
Check
getAcceptableTokens
in class Check
TokenTypes
public int[] getRequiredTokens()
Check
getRequiredTokens
in class Check
TokenTypes
public void visitToken(DetailAST ast)
Check
visitToken
in class Check
ast
- the token to processpublic void leaveToken(DetailAST ast)
Check
leaveToken
in class Check
ast
- the token leavingCopyright © 2001–2022. All rights reserved.