UselessParentheses "&" and "+" operator precedence
A source code analyzer
Brought to you by:
adangel,
juansotuyo
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
Thanks!
This will be fixed with PMD 5.3.5.
Commit: https://github.com/pmd/pmd/commit/145780de9cf3a25c8b19d3bb640db4dc0426e1ba