Repository navigation
[java] Fix #6775: UselessParentheses: not reported on the right-hand side of an as… - #6776
Conversation
…signment statement pmd#6775
Updated testAssignmentRHS to use assertAll for unnecessary parentheses check.
|
this is one of my first contributions, I hope it is good It took a while to make the build work. |
|
Compared to main: (comment created at 2026-06-12 09:15:21+00:00 for db6fd39) |
adangel
left a comment
There was a problem hiding this comment.
Thanks for the PR!
Just one little request: Can you also add a test case to https://github.com/pmd/pmd/blob/main/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/codestyle/xml/UselessParentheses.xml, like
<test-code>
<description>#6775 False negative when on the right-hand side of an assignment statement</description>
<expected-problems>1</expected-problems>
<expected-linenumbers>6</expected-linenumbers>
<code><![CDATA[
public class After {
public static void main(String[] argv) {
int a; a = Integer.parseInt(argv[0]);
int b; b = Integer.parseInt(argv[1]);
double value;
value = (a / b); // line 6 - expected violation
System.out.println(value);
}
}
]]></code>
</test-code>This helps us to be on the safe side, if we ever refactor the rule and the unit tests would be rewritten completely - we still have this covered in integration-like rule test.
|
Hi @adangel, |
Describe the PR
This PR fixes a false negative where
UselessParentheseswas not reported on the right-hand side of a plain assignment statement (e.g.,value = (a / b);), even though it was correctly reported within a variable initializer (e.g.,double value = (a / b);).Root Cause
In
UselessParenthesesRule.java, theneedsParenthesesmethod checks if!(outer instanceof ASTExpression)to quickly returnNEVERfor outer contexts like variable initializers. However, becauseASTAssignmentExpressionis a subclass ofASTExpression, assignment statements bypassed this check and incorrectly fell through to a fallback that classified the parentheses asCLARIFYING(which is ignored by default).Solution
Added an explicit check for
outer instanceof ASTAssignmentExpressionalongside the non-expression check to ensure parentheses surrounding assignments are immediately evaluated asNEVERnecessary.Related issues
Ready?
./mvnw clean verifypasses (checked automatically by github actions)