Menu

#1407 UselessParentheses "&" and "+" operator precedence

PMD-5.3.5
closed
None
PMD
3-Major
Bug
<=5.3.3
UselessParentheses
2015-09-26
2015-09-18
No

Hi All!
Here is the precedence of operations in Java:
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html

In the below example only the eUseless expressions should be violated with UselessParentheses.
In the eGood expressions the parentheses aren't useless, but many of them are violated:

class ExampleClass {
  void exampleExpressions() {
    eUseless = (a++) + b;
    eUseless = (a--) + b;
    eUseless = (++a) + b;
    eUseless = (--a) + b;
    eUseless = (+a) + b;
    eUseless = (-a) + b;
    eUseless = (~a) + b;
    eUseless = (!a) + b;
    eUseless = (a * b) + c;
    eUseless = (a / b) + c;
    eUseless = (a % b) + c;
    eUseless = (a + b) + c;
    eGood = (a - b) + c;
    eGood = (a << b) + c;
    eGood = (a >> b) + c;
    eGood = (a >>> b) + c;
    eGood = (a < b) + c;
    eGood = (a > b) + c;
    eGood = (a <= b) + c;
    eGood = (a >= b) + c;
    eGood = (a instanceof b) + c;
    eGood = (a == b) + c;
    eGood = (a != b) + c;
    eGood = (a & b) + c;
    eGood = (a ^ b) + c;
    eGood = (a | b) + c;
    eGood = (a && b) + c;
    eGood = (a || b) + c;
    eGood = (a ? b : c) + d;
  }
}

Here is the actual rule in XPath:
https://pmd.github.io/pmd-5.3.3/pmd-java/rules/java/unnecessary.html#UselessParentheses

This part of the XPath cause the wrong violations:

//Expression/AdditiveExpression[not(./PrimaryExpression/PrimaryPrefix/Literal[@StringLiteral = 'true'])]/PrimaryExpression[1]/PrimaryPrefix/Expression[
    count(*)=1 and
    not(./CastExpression) and
    not(./ConditionalExpression) and
    not(./ShiftExpression)]

The above part should be:

//Expression/AdditiveExpression[not(./PrimaryExpression/PrimaryPrefix/Literal[@StringLiteral = 'true'])]/PrimaryExpression[1]/PrimaryPrefix/Expression[
    count(*)=1 and
    not(./CastExpression) and
    not(./AdditiveExpression[@Image = '-']) and
    not(./ShiftExpression) and
    not(./RelationalExpression) and
    not(./InstanceOfExpression) and
    not(./EqualityExpression) and
    not(./AndExpression) and
    not(./ExclusiveOrExpression) and
    not(./InclusiveOrExpression) and
    not(./ConditionalAndExpression) and
    not(./ConditionalOrExpression) and
    not(./ConditionalExpression)]

Best Regards,
Zsolt German

Related

Issues: #1426

Discussion

  • Andreas Dangel

    Andreas Dangel - 2015-09-26
    • status: open --> closed
    • assigned_to: Andreas Dangel
    • Milestone: New Tickets --> PMD-5.3.5
     

Log in to post a comment.