public class UnnecessaryParenthesesExtendedCheck
extends com.puppycrawl.tools.checkstyle.api.AbstractCheck
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_KEY_ASSIGN
Warning message key.
|
static String |
MSG_KEY_EXPR
Warning message key.
|
static String |
MSG_KEY_IDENT
Warning message key.
|
static String |
MSG_KEY_LITERAL
Warning message key.
|
static String |
MSG_KEY_RETURN
Warning message key.
|
static String |
MSG_KEY_STRING
Warning message key.
|
| Constructor and Description |
|---|
UnnecessaryParenthesesExtendedCheck() |
| Modifier and Type | Method and Description |
|---|---|
int[] |
getAcceptableTokens() |
int[] |
getDefaultTokens() |
int[] |
getRequiredTokens() |
void |
leaveToken(com.puppycrawl.tools.checkstyle.api.DetailAST ast) |
void |
setIgnoreCalculationOfBooleanVariables(boolean ignoreCalculationOfBooleanVariables)
Sets flag to IgnoreCalculationOfBooleanVariables.
|
void |
setIgnoreCalculationOfBooleanVariablesWithAssert(boolean ignoreCalculationOfBooleanVariablesWithAssert)
Sets flag to IgnoreCalculationOfBooleanVariablesWithAssert.
|
void |
setIgnoreCalculationOfBooleanVariablesWithReturn(boolean ignoreCalculationOfBooleanVariablesWithReturn)
Sets flag to IgnoreCalculationOfBooleanVariablesWithReturn.
|
void |
visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST ast) |
beginTree, clearMessages, destroy, finishTree, getFileContents, getLine, getLines, getMessages, getTabWidth, getTokenNames, init, isCommentNodesRequired, log, log, log, setFileContents, setTabWidth, setTokensfinishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeveritypublic static final String MSG_KEY_ASSIGN
public static final String MSG_KEY_EXPR
public static final String MSG_KEY_IDENT
public static final String MSG_KEY_LITERAL
public static final String MSG_KEY_RETURN
public static final String MSG_KEY_STRING
public int[] getDefaultTokens()
getDefaultTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheckpublic int[] getAcceptableTokens()
getAcceptableTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheckpublic int[] getRequiredTokens()
getRequiredTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheckpublic void visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST ast)
visitToken in class com.puppycrawl.tools.checkstyle.api.AbstractCheckpublic void leaveToken(com.puppycrawl.tools.checkstyle.api.DetailAST ast)
leaveToken in class com.puppycrawl.tools.checkstyle.api.AbstractCheckpublic final void setIgnoreCalculationOfBooleanVariables(boolean ignoreCalculationOfBooleanVariables)
ignoreCalculationOfBooleanVariables - if true ignore unnecessary parentheses check in calculation of
boolean.public final void setIgnoreCalculationOfBooleanVariablesWithReturn(boolean ignoreCalculationOfBooleanVariablesWithReturn)
ignoreCalculationOfBooleanVariablesWithReturn - if true ignore unnecessary parentheses check in calculation of
boolean with return state.public final void setIgnoreCalculationOfBooleanVariablesWithAssert(boolean ignoreCalculationOfBooleanVariablesWithAssert)
ignoreCalculationOfBooleanVariablesWithAssert - if true ignore unnecessary parentheses check in calculation of
boolean with assert stateCopyright © 2021. All rights reserved.