Class 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 around a + b are not needed.

    Since:
    1.8.0
    Author:
    Eric Roe, Antonenko Dmitriy
    • Constructor Detail

      • UnnecessaryParenthesesExtendedCheck

        public UnnecessaryParenthesesExtendedCheck()
    • Method Detail

      • getDefaultTokens

        public int[] getDefaultTokens()
        Specified by:
        getDefaultTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • getAcceptableTokens

        public int[] getAcceptableTokens()
        Specified by:
        getAcceptableTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • getRequiredTokens

        public int[] getRequiredTokens()
        Specified by:
        getRequiredTokens in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • visitToken

        public void visitToken​(com.puppycrawl.tools.checkstyle.api.DetailAST ast)
        Overrides:
        visitToken in class com.puppycrawl.tools.checkstyle.api.AbstractCheck
      • leaveToken

        public void leaveToken​(com.puppycrawl.tools.checkstyle.api.DetailAST ast)
        Overrides:
        leaveToken in class com.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