Class UnnecessaryParenthesesCheck
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.api.AutomaticBean
-
- com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
-
- com.puppycrawl.tools.checkstyle.api.AbstractCheck
-
- com.puppycrawl.tools.checkstyle.checks.coding.UnnecessaryParenthesesCheck
-
- All Implemented Interfaces:
Configurable,Contextualizable
public class UnnecessaryParenthesesCheck extends 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 rhsThe 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
intvariables, the parentheses arounda + bare not needed.
-
-
Field Summary
Fields Modifier and Type Field Description private intassignDepthDepth of nested assignments.private static int[]ASSIGNMENTSToken types for assignment operations.private static int[]LITERALSToken types for literals.private static intMAX_QUOTED_LENGTHThe maximum string length before we chop the string.static java.lang.StringMSG_ASSIGNA key is pointing to the warning message text in "messages.properties" file.static java.lang.StringMSG_EXPRA key is pointing to the warning message text in "messages.properties" file.static java.lang.StringMSG_IDENTA key is pointing to the warning message text in "messages.properties" file.static java.lang.StringMSG_LITERALA key is pointing to the warning message text in "messages.properties" file.static java.lang.StringMSG_RETURNA key is pointing to the warning message text in "messages.properties" file.static java.lang.StringMSG_STRINGA key is pointing to the warning message text in "messages.properties" file.private DetailASTparentToSkipUsed to test if logging a warning in a parent node may be skipped because a warning was already logged on an immediate child node.
-
Constructor Summary
Constructors Constructor Description UnnecessaryParenthesesCheck()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static java.lang.StringchopString(java.lang.String value)Returns the specified string chopped toMAX_QUOTED_LENGTHplus an ellipsis (...) if the length of the string exceedsMAX_QUOTED_LENGTH.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 booleanisExprSurrounded(DetailAST ast)Tests if the given expression node is surrounded by parentheses.private static booleanisInTokenList(int type, int... tokens)Check if the given token type can be found in an array of token types.private static booleanisSurrounded(DetailAST ast)Tests if the givenDetailASTis surrounded by parentheses.voidleaveToken(DetailAST ast)Called after all the child nodes have been process.voidvisitToken(DetailAST ast)Called to process a token.-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
beginTree, destroy, finishTree, getClassLoader, getFileContents, getLine, getLines, getTabWidth, getTokenNames, init, isCommentNodesRequired, log, log, setClassLoader, setFileContents, setMessages, setTabWidth, setTokens
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, log, setId, setSeverity
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
configure, contextualize, finishLocalSetup, getConfiguration, setupChild
-
-
-
-
Field Detail
-
MSG_IDENT
public static final java.lang.String MSG_IDENT
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
MSG_ASSIGN
public static final java.lang.String MSG_ASSIGN
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
MSG_EXPR
public static final java.lang.String MSG_EXPR
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
MSG_LITERAL
public static final java.lang.String MSG_LITERAL
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
MSG_STRING
public static final java.lang.String MSG_STRING
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
MSG_RETURN
public static final java.lang.String MSG_RETURN
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
MAX_QUOTED_LENGTH
private static final int MAX_QUOTED_LENGTH
The maximum string length before we chop the string.- See Also:
- Constant Field Values
-
LITERALS
private static final int[] LITERALS
Token types for literals.
-
ASSIGNMENTS
private static final int[] ASSIGNMENTS
Token types for assignment operations.
-
parentToSkip
private DetailAST parentToSkip
Used to test if logging a warning in a parent node may be skipped because a warning was already logged on an immediate child node.
-
assignDepth
private int assignDepth
Depth of nested assignments. Normally this will be 0 or 1.
-
-
Method Detail
-
getDefaultTokens
public int[] getDefaultTokens()
Description copied from class:AbstractCheckReturns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.- Specified by:
getDefaultTokensin classAbstractCheck- Returns:
- the default tokens
- See Also:
TokenTypes
-
getAcceptableTokens
public int[] getAcceptableTokens()
Description copied from class:AbstractCheckThe configurable token set. Used to protect Checks against malicious users who specify an unacceptable token set in the configuration file. The default implementation returns the check's default tokens.- Specified by:
getAcceptableTokensin classAbstractCheck- Returns:
- the token set this check is designed for.
- See Also:
TokenTypes
-
getRequiredTokens
public int[] getRequiredTokens()
Description copied from class:AbstractCheckThe tokens that this check must be registered for.- Specified by:
getRequiredTokensin classAbstractCheck- Returns:
- the token set this must be registered for.
- See Also:
TokenTypes
-
visitToken
public void visitToken(DetailAST ast)
Description copied from class:AbstractCheckCalled to process a token.- Overrides:
visitTokenin classAbstractCheck- Parameters:
ast- the token to process
-
leaveToken
public void leaveToken(DetailAST ast)
Description copied from class:AbstractCheckCalled after all the child nodes have been process.- Overrides:
leaveTokenin classAbstractCheck- Parameters:
ast- the token leaving
-
isSurrounded
private static boolean isSurrounded(DetailAST ast)
Tests if the givenDetailASTis surrounded by parentheses. In short, doesasthave a previous sibling whose type isTokenTypes.LPARENand a next sibling whose type isTokenTypes.RPAREN.- Parameters:
ast- theDetailASTto check if it is surrounded by parentheses.- Returns:
trueifastis surrounded by parentheses.
-
isExprSurrounded
private static boolean isExprSurrounded(DetailAST ast)
Tests if the given expression node is surrounded by parentheses.- Parameters:
ast- aDetailASTwhose type isTokenTypes.EXPR.- Returns:
trueif the expression is surrounded by parentheses.
-
isInTokenList
private static boolean isInTokenList(int type, int... tokens)Check if the given token type can be found in an array of token types.- Parameters:
type- the token type.tokens- an array of token types to search.- Returns:
trueiftypewas found intokens.
-
chopString
private static java.lang.String chopString(java.lang.String value)
Returns the specified string chopped toMAX_QUOTED_LENGTHplus an ellipsis (...) if the length of the string exceedsMAX_QUOTED_LENGTH.- Parameters:
value- the string to potentially chop.- Returns:
- the chopped string if
stringis longer thanMAX_QUOTED_LENGTH; otherwisestring.
-
-