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

    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.

    Author:
    Eric Roe