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.-
Property
tokens- tokens to check Type isjava.lang.String[]. Validation type istokenSet. Default value is: EXPR, IDENT, NUM_DOUBLE, NUM_FLOAT, NUM_INT, NUM_LONG, STRING_LITERAL, LITERAL_NULL, LITERAL_FALSE, LITERAL_TRUE, ASSIGN, BAND_ASSIGN, BOR_ASSIGN, BSR_ASSIGN, BXOR_ASSIGN, DIV_ASSIGN, MINUS_ASSIGN, MOD_ASSIGN, PLUS_ASSIGN, SL_ASSIGN, SR_ASSIGN, STAR_ASSIGN, LAMBDA, TEXT_BLOCK_LITERAL_BEGIN.
To configure the check:
<module name="UnnecessaryParentheses"/>
Which results in the following violations:
public int square(int a, int b){ int square = (a * b); //violation return (square); //violation } int sumOfSquares = 0; for(int i=(0); i<10; i++){ //violation int x = (i + 1); //violation sumOfSquares += (square(x * x)); //violation } double num = (10.0); //violation List<String> list = Arrays.asList("a1", "b1", "c1"); myList.stream() .filter((s) -> s.startsWith("c")) //violation .forEach(System.out::println);Parent is
com.puppycrawl.tools.checkstyle.TreeWalkerViolation Message Keys:
-
unnecessary.paren.assign -
unnecessary.paren.expr -
unnecessary.paren.ident -
unnecessary.paren.lambda -
unnecessary.paren.literal -
unnecessary.paren.return -
unnecessary.paren.string
- Since:
- 3.4
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
AutomaticBean.OutputStreamOptions
-
-
Field Summary
Fields Modifier and Type Field Description 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_LAMBDAA 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.
-
Constructor Summary
Constructors Constructor Description UnnecessaryParenthesesCheck()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method 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.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, clearMessages, destroy, finishTree, getFileContents, getLine, getLines, getMessages, getTabWidth, getTokenNames, 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
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
configure, contextualize, 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
-
MSG_LAMBDA
public static final java.lang.String MSG_LAMBDA
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
-
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
-
-