Skip to content

[java] UnusedAssignment: False positive about the ++ unary operator #6200

Closed

Description

@garydgregory

The Maven PMD plugin reports UnusedAssignment false positives about the ++ unary operator.

Affects PMD Version:

7.18.0 and 7.17.0.

Rule:

UnusedAssignment

Description:

Run:

git clone https://github.com/apache/commons-jexl.git
cd commons-jexl
git checkout 3e8bf7b293dc9877de00df1e0d58a0a2ad488fe9
mvn pmd:check -Dcommons.pmd-impl.version=7.18.0

Reports:

<violation beginline="881" endline="881" begincolumn="70" endcolumn="74" rule="UnusedAssignment" ruleset="Best Practices" package="org.apache.commons.jexl3.internal" class="Debugger" method="visit" variable="vars" externalInfoUrl="https://docs.pmd-code.org/pmd-doc-7.18.0/pmd_rules_java_bestpractices.html#unusedassignment" priority="3">
The updated value of variable 'nc' is never used
</violation>
<violation beginline="882" endline="882" begincolumn="75" endcolumn="79" rule="UnusedAssignment" ruleset="Best Practices" package="org.apache.commons.jexl3.internal" class="Debugger" method="visit" variable="predicate" externalInfoUrl="https://docs.pmd-code.org/pmd-doc-7.18.0/pmd_rules_java_bestpractices.html#unusedassignment" priority="3">
The updated value of variable 'nc' is never used
</violation>
<violation beginline="884" endline="884" begincolumn="70" endcolumn="74" rule="UnusedAssignment" ruleset="Best Practices" package="org.apache.commons.jexl3.internal" class="Debugger" method="visit" variable="step" externalInfoUrl="https://docs.pmd-code.org/pmd-doc-7.18.0/pmd_rules_java_bestpractices.html#unusedassignment" priority="3">
The updated value of variable 'nc' is never used
</violation>

Code Sample demonstrating the issue:

    @Override
    protected Object visit(final ASTForeachStatement node, final Object data) {
        final int form = node.getLoopForm();
        builder.append("for (");
        final JexlNode body;
        if (form == 0) {
            // for( .. : ...)
            accept(node.jjtGetChild(0), data);
            builder.append(" : ");
            accept(node.jjtGetChild(1), data);
            builder.append(") ");
            body = node.jjtGetNumChildren() > 2? node.jjtGetChild(2) : null;
        } else {
            // for( .. ; ... ; ..)
            int nc = 0;
            // first child is var declaration(s)
            final JexlNode vars = (form & 1) != 0 ? node.jjtGetChild(nc++) : null;
            final JexlNode predicate = (form & 2) != 0 ? node.jjtGetChild(nc++) : null;
            // the loop step
            final JexlNode step = (form & 4) != 0 ? node.jjtGetChild(nc++) : null;
            // last child is body
            body = (form & 8) != 0 ? node.jjtGetChild(nc) : null;
            if (vars != null) {
                accept(vars, data);
            }
            builder.append("; ");
            if (predicate != null) {
                accept(predicate, data);
            }
            builder.append("; ");
            if (step != null) {
                accept(step, data);
            }
            builder.append(") ");
        }
        // the body
        if (body != null) {
            accept(body, data);
        } else {
            builder.append(';');
        }
        return data;
    }

Expected outcome:

PMD reports a violation at lines 881, 882, and 884, but that's wrong. That's a false positive.

Running PMD through: Maven

openjdk version "21.0.9" 2025-10-21
OpenJDK Runtime Environment Homebrew (build 21.0.9)
OpenJDK 64-Bit Server VM Homebrew (build 21.0.9, mixed mode, sharing)

Apache Maven 3.9.11 (3e54c93a704957b63ee3494413a2b544fd3d825b)
Maven home: /opt/homebrew/Cellar/maven/3.9.11/libexec
Java version: 21.0.9, vendor: Homebrew, runtime: /opt/homebrew/Cellar/openjdk@21/21.0.9/libexec/openjdk.jdk/Contents/Home
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "15.7.1", arch: "aarch64", family: "mac"

****.local 24.6.0 Darwin Kernel Version 24.6.0: Mon Aug 11 21:15:09 PDT 2025; root:xnu-11417.140.69.701.11~1/RELEASE_ARM64_T6041 arm64

Docker version 28.4.0, build d8eb465

Activity

  1. added
    a:false-positive
    on Nov 1, 2025
  2. changed the title [-]False positive `UnusedAssignment` about the ++ unary operator[/-] [+][java] False positive `UnusedAssignment` about the ++ unary operator[/+] on May 16, 2026
  3. changed the title [-][java] False positive `UnusedAssignment` about the ++ unary operator[/-] [+][java] UnusedAssignment: False positive about the ++ unary operator[/+] on Jun 24, 2026
  4. UncleOwen commented on Jun 24, 2026

    @UncleOwen
    Member

    Smaller reproducer:

    public class Foo {
        private int foo(int x) {
            return x+1;
        }
    
        public int visit(int i) {
            int x = c ? foo(i++) : 17;
            return i;
        }
    }

    x is unused, i is used - but the message still mentions i.

  5. added this to the 7.27.0 milestone on Aug 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:false-positive

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions