Skip to content

[java] Fix #6775: UselessParentheses: not reported on the right-hand side of an as… - #6776

Merged
adangel merged 5 commits into
pmd:mainfrom
dweep-js:issue6775
Jun 12, 2026
Merged

adangel merged 5 commits into
pmd:mainfrom
dweep-js:issue6775

Conversation

@dweep-js

@dweep-js dweep-js commented Jun 10, 2026 •

Copy link
Copy Markdown
Contributor

Describe the PR

This PR fixes a false negative where UselessParentheses was 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, the needsParentheses method checks if !(outer instanceof ASTExpression) to quickly return NEVER for outer contexts like variable initializers. However, because ASTAssignmentExpression is a subclass of ASTExpression, assignment statements bypassed this check and incorrectly fell through to a fallback that classified the parentheses as CLARIFYING (which is ignored by default).

Solution

Added an explicit check for outer instanceof ASTAssignmentExpression alongside the non-expression check to ensure parentheses surrounding assignments are immediately evaluated as NEVER necessary.

Related issues

Ready?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests
  • Complete build ./mvnw clean verify passes (checked automatically by github actions)
  • Added (in-code) documentation (if needed)

dweep and others added 2 commits June 10, 2026 18:57
Updated testAssignmentRHS to use assertAll for unnecessary parentheses check.
@dweep-js

Copy link
Copy Markdown
Contributor Author

this is one of my first contributions, I hope it is good It took a while to make the build work.

@pmd-actions-helper

pmd-actions-helper Bot commented Jun 10, 2026 •

Copy link
Copy Markdown
Contributor

Documentation Preview

Compared to main:
This changeset changes 0 violations,
introduces 406 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 15 errors and 9 configuration errors.
There are 0 changed duplications, 0 new duplications and 0 removed duplications.
There are 0 changed CPD errors, 0 new CPD errors and 0 removed CPD errors.

Regression Tester Report

(comment created at 2026-06-12 09:15:21+00:00 for db6fd39)

@adangel adangel changed the title Fix: UselessParentheses: not reported on the right-hand side of an as… [java] Fix #6775: UselessParentheses: not reported on the right-hand side of an as… Jun 11, 2026

@adangel adangel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@dweep-js

Copy link
Copy Markdown
Contributor Author

Hi @adangel,
I have added the XML test case to UselessParentheses.xml and pushed the changes.
Thank You For Your Review, I am glad I could help!

@adangel adangel added this to the 7.26.0 milestone Jun 12, 2026

@adangel adangel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@adangel
adangel merged commit db6fd39 into pmd:main Jun 12, 2026
22 checks passed
adangel added a commit that referenced this pull request Jun 12, 2026
adangel added a commit that referenced this pull request Jun 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[java] UselessParentheses: False negative when on the right-hand side of an assignment statement

2 participants