Class 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 rhs
     boolean a = (x > 7 && y > 5)      // parens around expression
                 || z < 9;
     boolean b = (~a) > -27            // parens around ~a
                 && (a-- < 30);        // parens around expression
     

    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. The check is partially aware about operator precedence but unaware about operator associativity. It won't catch cases such as:

     int x = (a + b) + c; // 1st Case
     boolean p = true; // 2nd Case
     int q = 4;
     int r = 3;
     if (p == (q <= r)) {}

    In the first case, given that a, b, and c are all int variables, the parentheses around a + b are not needed. In the second case, parentheses are required as q, r are of type int and p is of type boolean and removing parentheses will give a compile-time error. Even if q and r were boolean still there will be no violation raised as check is not "type aware".

    The partial support for operator precedence includes cases of the following type:

     boolean a = true, b = true;
     boolean c = false, d = false;
     if ((a && b) || c) { // violation, unnecessary paren
     }
     if (a && (b || c)) { // ok
     }
     if ((a == b) && c) { // violation, unnecessary paren
     }
     String e = "e";
     if ((e instanceof String) && a || b) { // violation, unnecessary paren
     }
     int f = 0;
     int g = 0;
     if (!(f >= g) // ok
             && (g > f)) { // violation, unnecessary paren
     }
     if ((++f) > g && a) { // violation, unnecessary paren
     }
     

    Parent is com.puppycrawl.tools.checkstyle.TreeWalker

    Violation 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
    • Method Detail

      • getAcceptableTokens

        public int[] getAcceptableTokens()
        Description copied from class: AbstractCheck
        The 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:
        getAcceptableTokens in class AbstractCheck
        Returns:
        the token set this check is designed for.
        See Also:
        TokenTypes
      • isSurrounded

        private static boolean isSurrounded​(DetailAST ast)
        Tests if the given DetailAST is surrounded by parentheses.
        Parameters:
        ast - the DetailAST to check if it is surrounded by parentheses.
        Returns:
        true if ast is surrounded by parentheses.
      • isExprSurrounded

        private static boolean isExprSurrounded​(DetailAST ast)
        Tests if the given expression node is surrounded by parentheses.
        Parameters:
        ast - a DetailAST whose type is TokenTypes.EXPR.
        Returns:
        true if the expression is surrounded by parentheses.
      • checkExpression

        private void checkExpression​(DetailAST ast)
        Checks whether an expression is surrounded by parentheses.
        Parameters:
        ast - the DetailAST to check if it is surrounded by parentheses.
      • unnecessaryParenAroundOperators

        private static boolean unnecessaryParenAroundOperators​(DetailAST ast)
        Checks if conditional, relational, bitwise binary operator, unary and postfix operators in expressions are surrounded by unnecessary parentheses.
        Parameters:
        ast - the DetailAST to check if it is surrounded by unnecessary parentheses.
        Returns:
        true if the expression is surrounded by unnecessary parentheses.
      • checkConditionalOrRelationalOperator

        private static boolean checkConditionalOrRelationalOperator​(DetailAST ast)
        Check if conditional or relational operator has unnecessary parentheses.
        Parameters:
        ast - to check if surrounded by unnecessary parentheses
        Returns:
        true if unnecessary parenthesis
      • checkBitwiseBinaryOperator

        private static boolean checkBitwiseBinaryOperator​(DetailAST ast)
        Check if bitwise binary operator has unnecessary parentheses.
        Parameters:
        ast - to check if surrounded by unnecessary parentheses
        Returns:
        true if unnecessary parenthesis
      • isBitWiseBinaryOrConditionalOrRelationalOperator

        private static boolean isBitWiseBinaryOrConditionalOrRelationalOperator​(int type)
        Check if token type is bitwise binary or conditional or relational operator.
        Parameters:
        type - Token type to check
        Returns:
        true if it is bitwise binary or conditional operator
      • isLambdaSingleParameterSurrounded

        private static boolean isLambdaSingleParameterSurrounded​(DetailAST ast)
        Tests if the given node has a single parameter, no defined type, and is surrounded by parentheses. This condition can only be true for lambdas.
        Parameters:
        ast - a DetailAST node
        Returns:
        true if the lambda has a single parameter, no defined type, and is surrounded by parentheses.
      • chopString

        private static String chopString​(String value)
        Returns the specified string chopped to MAX_QUOTED_LENGTH plus an ellipsis (...) if the length of the string exceeds MAX_QUOTED_LENGTH.
        Parameters:
        value - the string to potentially chop.
        Returns:
        the chopped string if string is longer than MAX_QUOTED_LENGTH; otherwise string.