Class UnnecessaryParenthesesExtendedCheck
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.api.AutomaticBean
-
- com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
-
- com.puppycrawl.tools.checkstyle.api.AbstractCheck
-
- com.github.sevntu.checkstyle.checks.coding.UnnecessaryParenthesesExtendedCheck
-
- All Implemented Interfaces:
com.puppycrawl.tools.checkstyle.api.Configurable
,com.puppycrawl.tools.checkstyle.api.Contextualizable
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 arounda + b
are not needed.- Since:
- 1.8.0
- Author:
- Eric Roe, Antonenko Dmitriy
-
-
Field Summary
Fields Modifier and Type Field 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 Summary
Constructors Constructor Description UnnecessaryParenthesesExtendedCheck()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method 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)
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
beginTree, clearViolations, destroy, finishTree, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, log, log, log, setFileContents, setTabWidth, setTokens
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
-
-
-
-
Field Detail
-
MSG_KEY_ASSIGN
public static final String MSG_KEY_ASSIGN
Warning message key.- See Also:
- Constant Field Values
-
MSG_KEY_EXPR
public static final String MSG_KEY_EXPR
Warning message key.- See Also:
- Constant Field Values
-
MSG_KEY_IDENT
public static final String MSG_KEY_IDENT
Warning message key.- See Also:
- Constant Field Values
-
MSG_KEY_LITERAL
public static final String MSG_KEY_LITERAL
Warning message key.- See Also:
- Constant Field Values
-
MSG_KEY_RETURN
public static final String MSG_KEY_RETURN
Warning message key.- See Also:
- Constant Field Values
-
MSG_KEY_STRING
public static final String MSG_KEY_STRING
Warning message key.- See Also:
- Constant Field Values
-
-
Method Detail
-
getDefaultTokens
public int[] getDefaultTokens()
- Specified by:
getDefaultTokens
in classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
getAcceptableTokens
public int[] getAcceptableTokens()
- Specified by:
getAcceptableTokens
in classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
getRequiredTokens
public int[] getRequiredTokens()
- Specified by:
getRequiredTokens
in classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
visitToken
public void visitToken(com.puppycrawl.tools.checkstyle.api.DetailAST ast)
- Overrides:
visitToken
in classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
leaveToken
public void leaveToken(com.puppycrawl.tools.checkstyle.api.DetailAST ast)
- Overrides:
leaveToken
in classcom.puppycrawl.tools.checkstyle.api.AbstractCheck
-
setIgnoreCalculationOfBooleanVariables
public final void setIgnoreCalculationOfBooleanVariables(boolean ignoreCalculationOfBooleanVariables)
Sets flag to IgnoreCalculationOfBooleanVariables.- Parameters:
ignoreCalculationOfBooleanVariables
- if true ignore unnecessary parentheses check in calculation of boolean.
-
setIgnoreCalculationOfBooleanVariablesWithReturn
public final void setIgnoreCalculationOfBooleanVariablesWithReturn(boolean ignoreCalculationOfBooleanVariablesWithReturn)
Sets flag to IgnoreCalculationOfBooleanVariablesWithReturn.- Parameters:
ignoreCalculationOfBooleanVariablesWithReturn
- if true ignore unnecessary parentheses check in calculation of boolean with return state.
-
setIgnoreCalculationOfBooleanVariablesWithAssert
public final void setIgnoreCalculationOfBooleanVariablesWithAssert(boolean ignoreCalculationOfBooleanVariablesWithAssert)
Sets flag to IgnoreCalculationOfBooleanVariablesWithAssert.- Parameters:
ignoreCalculationOfBooleanVariablesWithAssert
- if true ignore unnecessary parentheses check in calculation of boolean with assert state
-
-